原生ajax请求方式:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);
xhr.withCredentials = true; //支持跨域发送cookies xhr.send();
jquery的ajax的post方法请求:
$.ajax({
type: "GET", //POST方式后台获取数据失败,默认jquery.ajax只支持get, url: "http://xxx.com/api/test", dataType: 'jsonp', xhrFields: { withCredentials: true }, //必须有,详见 http://www.w3.org/TR/XMLHttpRequest/#the-withcredentials-attribute crossDomain: true,
success:function(){
},
error:function(){
}
})
服务器端设置:
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *"); //*代表所有网站,可以指定对应授权访问网址 http://www.xxx.com