jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox ajax ajax

jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox


The reason for the error is the same origin policy. It only allows you to do XMLHTTPRequests to your own domain. See if you can use a JSONP callback instead:

$.getJSON( 'http://<url>/api.php?callback=?', function ( data ) { alert ( data ); } );


I used the following code on Django side to interpret the OPTIONS request and to set the required Access-Control headers. After this my cross domain requests from Firefox started working. As said before, the browser first sends the OPTIONS request and then immediately after that the POST/GET

def send_data(request):    if request.method == "OPTIONS":         response = HttpResponse()        response['Access-Control-Allow-Origin'] = '*'        response['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'        response['Access-Control-Max-Age'] = 1000        # note that '*' is not valid for Access-Control-Allow-Headers        response['Access-Control-Allow-Headers'] = 'origin, x-csrftoken, content-type, accept'        return response    if request.method == "POST":        # ... 

Edit: it seems to be that at least in some cases you also need to add the same Access-Control headers to the actual response. This can be a little bit confusing, since the request seems to succeed, but Firefox does not pass the contents of the response to the Javascript.


This mozilla developer center article describes various cross-domain request scenarios. The article seems to indicate that a POST request with content type of 'application/x-www-form-urlencoded' should be sent as a 'simple request' (with no 'preflight' OPTIONS request). I found , however, that Firefox sent the OPTIONS request, even though my POST was sent with that content type.

I was able to make this work by creating an options request handler on the server, that set the 'Access-Control-Allow-Origin' response header to '*'. You can be more restrictive by setting it to something specific, like 'http://someurl.com'. Also, I have read that, supposedly, you can specify a comma-separated list of multiple origins, but I couldn't get this to work.

Once Firefox receives the response to the OPTIONS request with an acceptable 'Access-Control-Allow-Origin' value, it sends the POST request.