Jquery和Django CSRF令牌 [英] Jquery and Django CSRF Token
问题描述
我有2个html页面。
I have 2 html Pages.
父页面和子页面。子页面包含一个提交按钮,在父页面上运行代码以提交Ajax消息。
A Parent Page and a Child Page. The Child Page Contains a Submit Button that runs code on the Parent Page to submit an Ajax message.
我使用$ .load()方法加载子页面,然后在该按钮被点击它运行一个$ .ajax .POST方法。这个post方法只传递一个JSON字符串到Python代码。
I load the child page using $.load() method and then when the button is clicked it runs a $.ajax .POST Method. This post method only passes a JSON String to the Python Code.
当我在除IE之外的任何浏览器上执行此操作都可以正常工作。但是当我在IE中运行这个代码我得到关于CSRF令牌的Python / Django错误。
When I do this on any browser except IE It works fine. However when I run this code in IE. I get Python / Django Errors about CSRF Tokens.
我认为原因是因为子页面只是刷新当前页面本身有人正在运行服务器端代码。
I Think the reason is because the child page is just a refresh of current page itself with the serverside code being run.
有人知道我应该如何去做这件事。
Does anyone know how I should go about getting this to work.
干杯
推荐答案
您没有通过 POST 传递csrf令牌。尝试做我在数据中所做的工作。那就是获取csrf令牌(或你自己的方法)并将它传递给你的参数。
You are not passing the csrf token with POST. Try doing what I have done in data. That is to fetch the csrf token (or your own method) and pass it in your arguments.
$.ajax({
url : url,
type: "POST",
data : {csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value},
dataType : "json",
success: function( data ){
// do something
}
});
这篇关于Jquery和Django CSRF令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!