如何在 django 中使用 $.post ?
如何在 Django 中使用 jquery.post()
方法?
这就是我想做的:
var postdata={
'username':$('#login-email').val(),
'password':$('#login-password').val()
}
$.post('/login/',postdata)
How do I CSRF protected this in django?有没有办法将 CSRF 令牌添加到发布数据中?
How can I use the jquery.post()
method in Django?
This is what I am trying to do:
var postdata={
'username':$('#login-email').val(),
'password':$('#login-password').val()
}
$.post('/login/',postdata)
How do I CSRF protect this in django? Is there a way to add to the CSRF token to the post data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。我相信它存储在
{{ csrf_token }}
中。因此,您可能需要仔细检查名称,但这应该是正确的。
Yes. I believe it's stored in
{{ csrf_token }}
. So, just doYou might have to double check the names, but that should be right.
我通常将包含此内容的文件引用到我希望能够发出 AJAX 请求的每个页面:
I usually refer a file with this content to every page I want to be able to make AJAX requests:
尽管您没有在示例中提供 html,但可以安全地假设您使用的是
Although you didn't provide your html in your example is it safe to assume that you are using a
<form>
? If so, add your CSRF token template tag to your form and call.serialize()
on your form.Django 中的
contrib
模块有一个 CSRF 模块 您可以使用。至于您的问题如何发送
POST
,只要您正确映射了URL,请求就会发送到它。您可以通过检查请求对象上的request.POST
来专门处理POST
请求。The
contrib
module in Django has a CSRF module that you can use.As to your question how to send a
POST
, as long as you have the URL mapped properly requests will get sent to it. You can handle aPOST
request specifically by checkingrequest.POST
on the request object.