最近修改Vue接口,因为之前使用的是ThinkPHP,直接就用现成的 input() 接收数据了,没仔细研究怎么传值的。
今天写原生PHP代码,使用 $_POST 死活接收不到 Vue 传过来的数据。
查了下,原来 Vue 发送过来的是 Json 数据。
Accept:application/json
要么修改发送的header,要么修改php接收数据的方式
我采用的是后者
php中的 file_get_contents("php://input");
使用 php://input 即可正常接收 Vue Post 过来的参数。
具体代码如下:
$post = file_get_contents('php://input'); $decoded_post = json_decode($post); //var_dump($decoded_post); $live=$decoded_post->live;