我试图提交一个JSON格式的AJAX表单。在
但是每次提交表单时,我都会重定向到表单操作页面。在
HTML
{% csrf_token %}Username: {{accform.username}}
Password: {{accform.password}}
Jquery
^{pr2}$
我尝试过的上述代码的另一种选择是:$("#acc").submit(function(){
username = $(this).find("#id_username").val();
password = $(this).find("#id_password").val();
var arr = { "X-CSRFToken": getCookie("csrftoken"), "username": username, "password": password};
$.ajax({
url: '/myaccount/',
type: 'POST',
data: JSON.stringify(arr),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert(msg);
}
});
return false;
});
我还注意到,只有在获得csrftokencookie时才会被重定向。在
为什么?在