I\'m sending data cross domain via a POST request but the response isn\'t working, specifically, jQuery\'s success handler never gets called.
Stuff being used: Djang
Ok, so I believe the correct way to do things is this:
if request.method == "POST":
response = HttpResponse(simplejson.dumps(data),mimetype='application/json')
response['Access-Control-Allow-Origin'] = "*"
return response
elif request.method == "OPTIONS":
response = HttpResponse("")
response['Access-Control-Allow-Origin'] = "*"
response['Access-Control-Allow-Methods'] = "POST, OPTIONS"
response['Access-Control-Allow-Headers'] = "X-Requested-With"
response['Access-Control-Max-Age'] = "1800"
else:
return HttpResponseBadRequest()
This is based on the documentation I dug up from Mozilla on preflighted requests.
So, what I believe will happen is this:
OPTIONS
is sent with X-Requested-With
set to XMLHttpRequest
I believe this is necessary to allow Javascript access to anything, along with an Origin
header.X-Requested-With
thing". I'm saying that OPTIONS
and POST
are allowed and that this response should be cached for 30 mins.Allow-Methods
and Allow-Headers
but according to the exchange in the above linked documentation this isn't needed. This makes sense, the access check has already been done.Allow-Origin
field for validity, this being on the request such as POST
. If this passes, the client can have access to the data, if not, the request has already completed but the browser denies the actual client side application (Javascript) access to that data.I believe that is a correct summary of what is going on and in any case it appears to work. If I'm not right, please shout.