我正在尝试在我的客户端和位于两个不同域的服务器之间进行CORS请求。
服务器标头已设置为以下内容 -
Access-Control-Allow-Headers:Content-Type, Authorization, Content-Length, X-Requested-With
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin:*
如果我点击服务器网址(http://server.com)
,我可以看到响应标头现在,当我使用jquery将数据和方法发送到服务器进行处理并获取数据时,我得到以下内容
Failed to load http://server.com/event/live: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3016' is therefore not allowed access. The response had HTTP status code 502.
Jquery代码是 -
callMyAPI : function(pUrl,pType,pData,callback){
var apiURL = API_HOST+pUrl;
jQuery.ajax({
xhr: function() {
var xhr = jQuery.ajaxSettings.xhr();
xhr.upload.onprogress = function(e) {
if (typeof callback == "function"){
var percentComplete = parseInt((e.loaded / e.total)*100);
callback('progress',percentComplete);
}
};
xhr.addEventListener("progress", function(e){
if (typeof callback == "function"){
if (e.lengthComputable) {
var percentComplete = parseInt((e.loaded / e.total)*100);
callback('progress',percentComplete);
}
}
}, false);
return xhr;
},
url: apiURL,
data: pData,
type: pType,
beforeSend: function(xhr){
xhr.withCredentials = true;
},
crossDomain: true,
contentType: 'application/json',
dataType: 'json',
success: function(data) {
if (typeof callback == "function"){
callback('success',data);
}
},
complete: function(){
if (typeof callback == "function"){
callback('complete');
}
}
}).fail(function(xhr, status, error) {
if (typeof callback == "function"){
callback('fail',error);
}
});
},
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:0)
这与你的jquery代码无关。是阻止呼叫的浏览器。 我建议您确保关注,
http://server.com/event/live
路径上出现了相同的标头。