ajax - jquery post request + response in Django -
I am trying to request a post with jquery in Django. Unfortunately I have not been successful yet: Actually I see only one post request in Firebug, which is 200 and I get the following results (new to any image posting too):
Can anyone help me find out what I
In the FYI, I have commented on the CSRF line to overcome that issue. Time Being:
"# django.middleware.csrf.CsrfViewMiddleware ',"
views.py:
django import or json presented .shortcuts import django.http import HttpResponse def index (request) to submit returns (request, 'index.html') def ajax_test (request): If request.method == 'POST' and Requestkis_ajax () : Name = request POST ['name'] city = request.POST ['city'] message = name + 'city in return' httpresponse (json.dumps ({'message': message}) Return submission (request, 'Ajax.html ')
index.html:
& lt; Script src = "http://ajax.googleapis.co M / ajax / libs / jQuery / 1.11.1 / jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Script src = "/ static / js / ajax.js" & gt; & Lt; / Script & gt; & Lt; Script src = "http://getbootstrap.com/dist/js/bootstrap.js" & gt; & Lt; / Script & gt; & Lt; Form id = "my_form" action = "" method = "post" & gt; & Lt; Input type = "submit" value = "send" & gt; & Lt; / Form & gt;
ajax.js:
$ (document) .ready (function () {$ ("# my_form"). Submit (function () { $ .post ("data:" + data + "\ n status:" + status ";}). (" ", {Name:" donald duck ", city:" duckburg ",}, function (data, status) .fail (function (xhr) {console.log ( "error:" + xhr.statusText); alert ( "error:" + xhr.statusText);}); return;});}); Django.conf. urls import patterns, urlpatterns = patterns to see URL import elections ( '', url (r '^ $', Views.index, name = 'index'), url (r '^ ajax / $', views.ajax_test , Name = "ajax"),)
I Appropriate kindle Try it to set your post URL.
Comments
Post a Comment