ajax - jquery post request + response in Django -
i trying create post request jquery in django. unfortunately haven't succeeded yet. indeed : see in firebug post request status 200 , next result (too new post image):
https://drive.google.com/a/essec.edu/file/d/0b5magnfqfkbxevbnv1pfzc1sbk0/view?usp=sharing
could help me find have done wrong?
my code comes here : need simple working ajax illustration django forms , think should correct.
fyi, in settings.py have commented csrf line set away issue time beeing :
" #'django.middleware.csrf.csrfviewmiddleware',"
views.py:
import json django.shortcuts import render django.http import httpresponse def index(request): homecoming render(request, 'index.html') def ajax_test(request): if request.method == 'post' , request.is_ajax(): name = request.post['name'] city = request.post['city'] message = name + ' lives in ' + city homecoming httpresponse(json.dumps({'message': message})) homecoming render(request, 'ajax.html')
index.html:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="/static/js/ajax.js"></script> <script src="http://getbootstrap.com/dist/js/bootstrap.js"></script> <form id="my_form" action="" method="post"> <input type="submit" value="send"> </form>
ajax.js:
$(document).ready(function(){ $("#my_form").submit(function(){ $.post("", {name:"donald duck", city:"duckburg", }, function(data,status){ alert("data: " + info + "\nstatus: " + status); }) .fail(function(xhr) { console.log("error: " + xhr.statustext); alert("error: " + xhr.statustext); }); homecoming false; }); });
urls.py:
from django.conf.urls import patterns, url polls import views urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^ajax/$', views.ajax_test, name="ajax"), )
i bet form action. seek setting url of post.
jquery ajax django
No comments:
Post a Comment