ajax 跨域 Access-Control-Allow-Origin

相信大家在项目开发中遇到过 很多跨域问题~ 

那我来说说 ajax(非jsonp) 请求跨域吧 需要后端配合

后端程序 需要在头信息加入 跨域允许

header('Access-Control-Allow-Origin:'.$origin);  //$origin允许的域名
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Credentials:true');//允许cookie获取

前端程序

$.ajaxSetup({
xhrFields: {
withCredentials: true
}
});
$.ajax({
'url':"http://user.cli.me/api/qrcode_template/gettpl",
'data':{'data1':data1,'data2':data2,'type':'first'},
'type':'post',
'dataType':'json',
'success':function(ret){
if(ret.status=='1'){
callback(ret.data);
}else{
alert(ret.msg);
}
}
})

原生的话设置 XMLHttpRequest对象 的 

withCredentials属性为true
原文地址:https://www.cnblogs.com/bobogoodgoodstudy/p/4335045.html