csrf_token 没有找到 django ajax
csrf_token not found django ajax
<script>
function post(e) {
data = $("#form_add_post").serialize();
$.post( "/post/", function( data ) {
alert("posted");
});
return false;
}
function addPost(){
$(".matter").html("<form id='form_add_post' onsubmit='return post(event);'>{% csrf_token %} <table> <tr> <td class='heading'>Title: </td> <td class='box'><input type='text' name='title'></td> </tr> <tr> <td colspan='2'><textarea class='data' name='content'></textarea></td> </tr> </table> <input id='submitt1' type='submit'> </form>");
}
</script>
我正在尝试执行 AJAX post 呼叫。我也放置了 csrf_token。我已经交叉检查了正在发送的数据。它显示了全部详细信息,包括 csrf_token.
我的数据:
csrfmiddlewaretoken=foYqu9LrR25AomOmcFkaEicmN3CU2wcRNVg1gRPgl2F9XfL6IWerpbSK6TUKd4Ke&title=Tester&content=hhgj%3B%3Bhghjbjn
但是我收到 403 错误显示
CSRF verification failed. Request aborted.
根据 Laravel Docs,您应该在 Ajax 请求中添加 CSRF 令牌,如下所示:
HTML代码:
<meta name="csrf-token" content="{{ csrf_token() }}">
JS代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
In case of Django - You need to add the {% csrf_token %}
template
tag as a child of the form element in your Django template. See this -
Question
希望对您有所帮助!
你打错了$.post
。如果您查看开发人员工具中的请求,我想您会发现 post 数据为空。
第二个参数应该是请求数据,第三个参数是success
回调。
$.post("/post/",
data,
function (response_data) {
alert("posted");
}
);
一旦你解决了这个问题,the Django docs 将展示如何将 CSRF 令牌作为 header 包含在 ajax 请求中,这样你就不必将它包含在表格 body.
尝试这样请求
$.ajax({
type:form.attr('method'),
url:form.attr('action'),
data:form.serialize(),
success: function(){
...
}
});
<script>
function post(e) {
data = $("#form_add_post").serialize();
$.post( "/post/", function( data ) {
alert("posted");
});
return false;
}
function addPost(){
$(".matter").html("<form id='form_add_post' onsubmit='return post(event);'>{% csrf_token %} <table> <tr> <td class='heading'>Title: </td> <td class='box'><input type='text' name='title'></td> </tr> <tr> <td colspan='2'><textarea class='data' name='content'></textarea></td> </tr> </table> <input id='submitt1' type='submit'> </form>");
}
</script>
我正在尝试执行 AJAX post 呼叫。我也放置了 csrf_token。我已经交叉检查了正在发送的数据。它显示了全部详细信息,包括 csrf_token.
我的数据:
csrfmiddlewaretoken=foYqu9LrR25AomOmcFkaEicmN3CU2wcRNVg1gRPgl2F9XfL6IWerpbSK6TUKd4Ke&title=Tester&content=hhgj%3B%3Bhghjbjn
但是我收到 403 错误显示
CSRF verification failed. Request aborted.
根据 Laravel Docs,您应该在 Ajax 请求中添加 CSRF 令牌,如下所示:
HTML代码:
<meta name="csrf-token" content="{{ csrf_token() }}">
JS代码:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
In case of Django - You need to add the
{% csrf_token %}
template tag as a child of the form element in your Django template. See this - Question
希望对您有所帮助!
你打错了$.post
。如果您查看开发人员工具中的请求,我想您会发现 post 数据为空。
第二个参数应该是请求数据,第三个参数是success
回调。
$.post("/post/",
data,
function (response_data) {
alert("posted");
}
);
一旦你解决了这个问题,the Django docs 将展示如何将 CSRF 令牌作为 header 包含在 ajax 请求中,这样你就不必将它包含在表格 body.
尝试这样请求
$.ajax({
type:form.attr('method'),
url:form.attr('action'),
data:form.serialize(),
success: function(){
...
}
});