vue 跨域访问tp3接口,tp3 I()方法无法获取前端请求数据

跨域设置:

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;
}
}

猜你喜欢

转载自www.cnblogs.com/yuanlai/p/10728293.html
tp3