CORS
// 生產(chǎn)環(huán)境 * 應(yīng)改為具體域名
header('Access-Control-Allow-Origin: *');
if ($_SERVER["REQUEST_METHOD"] == 'OPTIONS')
{
header('Access-Control-Allow-Headers: X-Requested-With,Content-Type');
exit();
}
Ajax跨域不攜帶COOKIE
PHP
header('Access-Control-Allow-Origin: domain.com');
// 此處為true,上面不可為 *
header('Access-Control-Allow-Credentials: true');
if ($_SERVER["REQUEST_METHOD"] == 'OPTIONS')
{
header('Access-Control-Allow-Headers: X-Requested-With,Content-Type');
exit();
}
jQuery
$.ajax({
url: "http://api.domain.com",
xhrFields: {withCredentials: true},
dataType: 'JSON',
type: 'POST',
data: {id: 1},
success: function(response)
{
console.log('Success')
},
error: function()
{
console.log('Error')
}
})