I wrote a simple Django view:
from django.http import HttpResponse
from django.utils import simplejson
from django.views.decorators.csrf import csrf_exempt # just add
@csrf_exempt
def handleRequest(request, intervention_code):
result = {}
norm_str = ''
........
return HttpResponse(simplejson.dumps(result), mimetype='application/json')
The view uses no template, just queries the database and returns the data in JSON format.
I can retrieve the JSON object easily doing http://myserver/myurl
Unfortunately, I have to use a javascript library to make a POST AJAX request to get the same JSON object and I run into the typical Django CSRF failure.
When I do: curl -d "sometext=foobar" http://myserver/myurl/ I got the same result.
I implemented all recommended CSRF middleware from Django doc, and still get the error.
Django’s documentation describes what you have to do to get CSRF working with Ajax.