我想用Django中的jquery发出post请求。不幸的是,我还没有成功。事实上,我只看到萤火虫POST请求,其状态是200,我得到以下结果(太新张贴任何图像):Django中的jquery post请求+响应
https://drive.google.com/a/essec.edu/file/d/0B5MagNFQFkbXeVBNV1pfZC1sbk0/view?usp=sharing
谁能帮我找到了我做错了?
我的代码来自这里: Need a simple working ajax example for django forms,所以我认为它应该是正确的。
仅供参考,settings.py中我评论过的CSRF线放好这个问题的时间beeing:
“# 'django.middleware.csrf.CsrfViewMiddleware',”
views.py :
import json
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'index.html')
def ajax_test(request):
if request.method == 'POST' and request.is_ajax():
name = request.POST['name']
city = request.POST['city']
message = name + ' lives in ' + city
return HttpResponse(json.dumps({'message': message}))
return 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: " + data + "\nStatus: " + status);
})
.fail(function(xhr) {
console.log("Error: " + xhr.statusText);
alert("Error: " + xhr.statusText);
});
return false;
});
});
urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^ajax/$', views.ajax_test, name="ajax"),
)