简体   繁体   English

为什么成功的jQuery跨域发布请求无法触发成功处理程序?

[英]Why is successful jQuery cross domain post request not firing success handler?

I am making the following JQuery cross domain ajax post request from a phonegap (appgyver steroids) app. 我正在从phonegap(appgyver类固醇)应用程序发出以下JQuery跨域ajax发布请求。

function do_something(callback_method, some_vars)
{
    var stringified_some_vars = JSON.stringify(some_vars);
    $.ajax({ 
    type: "POST",
    url:"http://www.somedomain.com/endpoint",
    data: {'some_vars' : stringified_some_vars},
    async: true,
    dataType: 'json', 
    crossDomain: true,
    xhrFields: {
      withCredentials: true
    },
    success: function(result) 
        {
            var myObject = $.parseJSON(result);
            callback_method(myObject);
        },
    error: function(fail)
    {
        supersonic.logger.debug(fail);
    }
    });
}

The post request is successfully sent to the server (Google Appengine - Python) - ie the server fires the relevant method. 发布请求已成功发送到服务器(Google Appengine-Python)-即服务器触发相关方法。 However, when the server response is received the jQuery Ajax method doesn't fire the success handler - it instead fires the error handler. 但是,当收到服务器响应时,jQuery Ajax方法不会触发成功处理程序,而是触发错误处理程序。 The error text prints to console as 错误文本以以下方式打印到控制台

{"readyState":0,"responseText":"","status":0,"statusText":"error"}

The headers in the json response from the server are as follows: 来自服务器的json响应中的标头如下:

Content-Length: 0 
Cache-Control: no-cache 
Access-Control-Allow-Origin: * 
Content-Type: application/json

the content of the response is as expected. 响应的内容符合预期。 and is written using 并使用

text_to_send = json.dumps(python_dict)
self.response.headers.add_header('Access-Control-Allow-Origin', '*')
self.response.headers['Content-Type'] = 'application/json'
self.response.write(text_to_send)

It's not clear to me where this error is coming from. 我不清楚此错误来自何处。 Allowing cross domain requests doesn't seem to have fixed the issue. 允许跨域请求似乎并未解决该问题。 jsonp GET requests work fine - but obviously these aren't allowed for POST requests. jsonp GET请求可以正常工作-但显然,POST请求不允许使用这些请求。 Could anyone tell me where I'm going wrong? 谁能告诉我我要去哪里错了?

Edit 1 编辑1

Following the suggestion of @Faisal I adjusted the server code as follows 遵循@Faisal的建议,我如下调整了服务器代码

    from urlparse import urlparse
    uri = urlparse(self.request.referer)
    origin = '{}://{}'.format(uri.scheme, uri.netloc)
    self.response.headers.add_header('Access-Control-Allow-Origin', origin)
    self.response.headers['Content-Type'] = 'application/json'

The headers are now 标头现在

Content-Length: 0 
Cache-Control: no-cache 
Access-Control-Allow-Origin: http://localhost 
Content-Type: application/json 

However the same error is encountered 但是遇到相同的错误

I think if it's withCredentials: true you need your origin to be exact match instead of wildcard (*). 我认为,如果使用withCredentials:是的,则需要您的来源是完全匹配的,而不是通配符(*)。 Here is a quick code to get it from referer. 这是一个从引荐来源获取代码的快速代码。 But you should probably also check if its one of the allowed origins: 但您可能还应该检查其是否为允许的来源之一:

from urlparse import urlparse

uri = urlparse(self.request.referer)
origin = '{}://{}'.format(uri.scheme, uri.netloc)

Edit 编辑

Try adding: 尝试添加:

self.response.headers.add_header('Access-Control-Allow-Credentials', 'true')

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

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