以下是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);
});