跨域请求被阻止:同源策略禁止读取某些URI上的远程资源
问题描述:
我是在ajax请求之后编写的:
I wrote this following ajax request:
$.ajax({
type : "POST",
processData:false,
crossDomain:true,
crossOrigin:true,
contentType:false,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
},
url : 'my URI',
data: formData,
success : function(receivedData) {
console.log("SUCCESS: ");
alert(receivedData);
}
});
但是在回复中,我通过浏览器收到以下消息: 跨域请求被阻止:同源策略禁止读取我的URI上的远程资源. (原因:CORS标头"Access-Control-Allow-Origin"缺失).
But in the reply I'm getting this following message in by browser : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at my URI. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
答
如果下面的代码不起作用,请尝试下面的代码,您需要实现服务器端cors启用
Try below code if it will not work then you need to implement server side cors enabling
$.ajax({
type : "POST",
processData:false,
crossDomain:true,
crossOrigin:true,
contentType:false,
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
},
header:{'Access-Control-Allow-Origin': '*'},
url : 'my URI',
data: formData,
success : function(receivedData) {
console.log("SUCCESS: ");
alert(receivedData);
}
});
然后您可以像这样使用ajax调用
then you can use ajax call like that
$.ajax({
url: 'http:ww.abc.com?callback=?',
dataType: 'JSONP',
jsonpCallback: 'callbackFnc',
type: 'GET',
async: false,
crossDomain: true,
success: function () { },
failure: function () { },
complete: function (data) {
if (data.readyState == '4' && data.status == '200') {
errorLog.push({ IP: Host, Status: 'SUCCESS' })
}
else {
errorLog.push({ IP: Host, Status: 'FAIL' })
}
}
});