跨域设置:
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header('Access-Control-Allow-Credentials', true);
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding, X-Token, Session-Token");
查看前端vue request header,content-type:application/json
原因:I()方法无法获取json数据
解决办法:
1,前端将请求头json 改为application/x-www-form-urlencoded,并更改数据传输方式
2、后端处理,将json数据强行赋值给$_POST,或者$_GET
if(!I('')) {
$request_data = file_get_contents('php://input');
$request_data = json_decode($request_data, true);
foreach ($request_data as $key => $value) {
$_POST[$key]=$value;
}
}