[英]CORS issue with localhost and put method
I'm trying to make a CORS requests to communicate between my client and server placed in two different domain.我正在尝试发出 CORS 请求,以便在位于两个不同域中的客户端和服务器之间进行通信。
The server headers has been set to the following -服务器标头已设置为以下内容 -
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:*
I can see the response header coming through if I hit the server url ( http://server.com )如果我点击服务器 url ( http://server.com ),我可以看到响应标头通过
Now when I use jquery to send the data and method to the server to process and get back the data then I get the following现在,当我使用 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 code is -- 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);
}
});
},
Any help is highly appreciated.任何帮助都受到高度赞赏。 Thanks in advance.提前致谢。
This has to do nothing with your jquery code.这与您的 jquery 代码无关。 It is the browser that blocks the calls.阻止呼叫的是浏览器。 I suggest that you ensure following,我建议你确保遵循,
http://server.com/event/live
route as mentioned.如上所述,相同的标头出现在http://server.com/event/live
路由上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.