Ajax POST and Django Tastypie Ajax POST and Django Tastypie ajax ajax

Ajax POST and Django Tastypie


You are explicitly declaring your content type in your call to curl, but you are not being specific on your jQuery.ajax() call.

Update your JavaScript to define exactly what the content type is going to be:

$.ajax({  type: 'POST',  url: 'http://localhost:8000/geo/api/geolocation/',  data: '{"latlong": "test"}',  success: latlongSaved(),  dataType: "application/json",  processData:  false,  contentType: "application/json"});


I added XS_SHARING_ALLOWED_HEADERS to the middleware and that solved the problem.

https://gist.github.com/1164697


Add XsSharing (https://gist.github.com/1164697) to settings.py:

MIDDLEWARE_CLASSES = [    ...,    'django-crossdomainxhr-middleware.XsSharing']

Then use the following javascript to make an ajax call:

$.ajax({  type: 'POST',  url: 'http://localhost:8000/geo/api/geolocation/',  data: '{"latlong": "test"}',  success: latlongSaved(),  contentType:'application/json',  dataType: 'application/json',  processData: false,});

Notice that data must be a well-formed JSON string, otherwise jQuery will silently ignore the ajax call and do nothing.

What's behind the scenes is that the ajax call will firstly send out OPTIONS /geo/api/geolocation/. Because the response header is modified by XsSharing middleware, jQuery will issue another POST /geo/api/geolocation request that does the actual creation.