3
我提出AJAX調用象下面這樣:故宮(CSRF令牌丟失或不正確。):
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'),
]
我收到「禁(CSRF令牌丟失或不正確。):/ issuebook「ajax調用錯誤。
在Ajax調用的CSRF令牌越來越呈現爲:
var data_dict = {'user':{{ user.id }}, 'bookId':that.id, 'csrfmiddlewaretoken':'fSSdu8dJ4FO6FvDz8eU5ISzOewRYyGbC'};
$.ajax({
type: 'POST',
url:"/issuebook",
data:data_dict,
contentType: false,
success:function(response)
{
}
});
您剛剛通過字符串''{{csrf_token}}'作爲'csrfmiddlewaretoken',並且您的ajax調用無法與相對的匹配。相反,您可以在您的調用函數中從您的html手動獲取'csrf'標記的哈希值。 – Kasramvd
也在問題中添加呈現的HTML模板。 – v1k45
@ v1k45我在編輯的問題中添加了呈現的{{csrf_token}}。除此之外,我只是在正常工作的模板中呈現幾個字符串值 – ankit