react fetch 跨域怎么解决
1个回答
2018-01-09 · 知道合伙人软件行家
关注
展开全部
以下是php部分的允许跨域域名访问的设置
$origin = isset($_SERVER['HTTP_ORIGIN'])?$_SERVER['HTTP_ORIGIN']:''; $allowOrigin = [ 'http://www.xxx.com', 'http://xxx.com'
]; if (in_array($origin, $allowOrigin)) {
header('Access-Control-Allow-Origin: ' . $origin);
}
header('Access-Control-Allow-Methods: PUT,POST,GET,DELETE,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Content-Type, Accept');
以下是fetch部分的ajax请求
let postData = {a:'b'};
fetch('http://data.xxx.com/Admin/Login/login', {
method: 'POST',
mode: 'cors',
credentials: 'include',
headers: { 'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify(postData)
}).then(function(response) {
console.log(response);
});
$origin = isset($_SERVER['HTTP_ORIGIN'])?$_SERVER['HTTP_ORIGIN']:''; $allowOrigin = [ 'http://www.xxx.com', 'http://xxx.com'
]; if (in_array($origin, $allowOrigin)) {
header('Access-Control-Allow-Origin: ' . $origin);
}
header('Access-Control-Allow-Methods: PUT,POST,GET,DELETE,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Content-Type, Accept');
以下是fetch部分的ajax请求
let postData = {a:'b'};
fetch('http://data.xxx.com/Admin/Login/login', {
method: 'POST',
mode: 'cors',
credentials: 'include',
headers: { 'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify(postData)
}).then(function(response) {
console.log(response);
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询