gpt4 book ai didi

django - 禁止(CSRF token 丢失或不正确。):

转载 作者:行者123 更新时间:2023-12-04 12:05:48 27 4
gpt4 key购买 nike

我正在像下面这样进行ajax调用:

var data_dict = {'user':{{ user.id }}, 'bookId':that.id, 'csrfmiddlewaretoken': '{{ csrf_token }}'};
$.ajax({
type: 'POST',
url:"/issuebook",
data:data_dict,
processData: false,
contentType: false,
success:function(response)
{
}
});

urls.py是:
urlpatterns = [
url(r'^$',views.checkLogin,name='checklogin'),
url(r'^mylibrary/(?P<pk>\d+)/(?P<user_name>[\w\-]+)$',login_required(views.MyLibrary.as_view()),name='mylibrary'),
url(r'^centrallibrary/(?P<pk>\d+)/(?P<user_name>[\w\-]+)$',login_required(views.CentralLibrary.as_view()),name='centrallibrary'),
url(r'^issuebook$',login_required(views.IssueBookView.as_view()),name='issuebook'),

]

我在ajax调用中收到“禁止(CSRF token 丢失或不正确。):/issuebook”错误。

Ajax调用中的csrf token 被呈现为:
var data_dict = {'user':{{ user.id }}, 'bookId':that.id, 'csrfmiddlewaretoken':'fSSdu8dJ4FO6FvDz8eU5ISzOewRYyGbC'};
$.ajax({
type: 'POST',
url:"/issuebook",
data:data_dict,
contentType: false,
success:function(response)
{
}
});

最佳答案

此错误是由ajax函数中的processDatacontentType选项引起的。删除这两个选项将解决此问题。

说明:
参数必须以Content-Type application/x-www-form-urlencoded urlencode的形式发送到Django。而如果您设置processData: false,则不会对POST参数进行编码,并且contentType: false将ajax POST请求发送为text/plain

关于django - 禁止(CSRF token 丢失或不正确。):,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36291186/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com