我有一个简单的表单来提交和测试ajax,但它不起作用,我看到它有任何问题,我搜索了很多,没有更多的点 - 至少我看不到;)
urls.py
中的
(r'^doctors/shahsavand/visiting/$','DrHub.views.visiting'),
(r'^doctors/shahsavand/visiting/add/$','DrHub.views.ajxTest')
第一个URL是指向主页面,主页面中有这个表格:
<form method='POST' action=".">
{% csrf_token %}
<ul>
<li>
<label for="start">Start Time: </label><input name="id_startTime" id="id_startTime" type="text" />
</li>
<li>
<label for="end">End Time: </label><input name="id_endTime" id="id_endTime" type="text" />
</li>
</ul>
<input type="submit" id="save_button" name="save_button" value="add" />
</form>
和本页中的ajax代码:
<script type='text/javascript' src='/static/DrHub/doctors/shahsavand/js/jquery-1.4.1.js'></script>
<script type="text/javascript">
$.ajax({
type:"POST",
url:"{% url DrHub.views.ajxTest %}",
data: {
'start': $('#id_startTime').val(),
'end': $('#id_endTime').val(),
'csrfmiddlewaretoken':$( "#csrfmiddlewaretoken" ).val()
},
success: function(data){
alert(data);
}
});
</script>
views.py
中的:
def ajxTest(request):
if request.is_ajax():
if request.method == 'POST':
return HttpResponse(simplejson.dumps({'message' : 'awesome'}), mimetype='application/javascript')
else:
return render_to_response('DrHub/doctors/nutrition/test.html', context_instance=RequestContext(request))
我这样做是为了测试是否调用了ajxTest view
:
def ajxTest(request):
if request.is_ajax():
raise Http404
else:
pass
和此:
def ajxTest(request):
if request.is_ajax():
raise pass
else:
Http404
但没有结果,听起来像我的ajax POST与ajxTest view
!!!
edit
当我检查萤火虫时,我得到的403 forbidden error
是CSRF
而我已经尝试了很多东西来解决它而没有结果:(