Put dataType: "json" in the jquery call. A javascript object would be appropriate.
$.ajax({ url: "/ajax/", type: "POST", data: name, cache:false, dataType: "json", success: function(resp){ alert ("resp: "+resp.name); } });
In Django, you must return a json-serialized dictionnary containing the data. Content_type must be application/json . In this case, the local residents trick is not recommended, as it is possible that some local variables cannot be serialized in json. This caused an exception. Also note that is_ajax is a function and should be called. In your case, this will always be true. I would also check request.method , not request.POST
import json def lat_ajax(request): if request.method == 'POST' and request.is_ajax(): name = request.POST.get('name') return HttpResponse(json.dumps({'name': name}), content_type="application/json") else : return render_to_response('ajax_test.html', locals())
UPDATE: As Jurudocs mentioned, csrf_token could also be a reason I would recommend reading: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
source share