Why is successful jQuery cross domain post request not firing success handler?
Question: I am making the following JQuery cross domain ajax post request from a
phonegap (appgyver steroids) app.

    
    
    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) - i.e. the server fires the relevant method. However, when the server
response is received the jQuery Ajax method doesn't fire the success handler -
it instead fires the error handler. 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:

    
    
    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. Could anyone tell me
where I'm going wrong?

## Edit 1

Following the suggestion of @Faisal I adjusted the server code as follows

    
    
        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


Answer: I think if it's withCredentials: true you need your origin to be exact match
instead of wildcard (*). 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')
    

