简体   繁体   English

本地主机和放置方法的 CORS 问题

[英]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,我建议你确保遵循,

  1. Same headers are coming on http://server.com/event/live route as mentioned.如上所述,相同的标头出现在http://server.com/event/live路由上。
  2. Try datatype as jsonp in xhr, if this fixes the problem, in that case it is certain that header response is not correct(either misspelled or something missing).在 xhr 中尝试将数据类型设为 jsonp,如果这解决了问题,在这种情况下,可以肯定标头响应不正确(拼写错误或缺少某些内容)。 jsonp is not recommended at all for type of project you are doing.对于您正在执行的项目类型,根本不建议使用 jsonp。
  3. Most importantly, if you are doing POST/PUT on that rout, ensure you respond back to OPTIONS type of calls (explore pre-flight calls)最重要的是,如果您在该路由上执行 POST/PUT,请确保您回复 OPTIONS 类型的呼叫(探索飞行前呼叫)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM