1
Archangel_SDY 2014-02-21 13:16:32 +08:00 1
要么你先下发一个token给客户端,要么你就干脆禁掉这个View的CSRF:
https://docs.djangoproject.com/en/1.5/ref/contrib/csrf/#django.views.decorators.csrf.csrf_exempt |
2
tamamaxox 2014-02-21 13:23:25 +08:00 via Android
求解,Ajax需要csrf吗
|
6
magine OP @Archangel_SDY 额……在stackoverflow上找到的文档页面和你一样,太感谢了。
|
7
zhwei 2014-02-21 16:32:29 +08:00 1
|
8
magine OP @Archangel_SDY
setting.py的MIDDLEWARE_CLASSES中加上 django.middleware.csrf.CsrfViewMiddleware view.py中对应的视图函数前加装饰器 @csrf_exempt |
9
otakustay 2014-02-21 21:01:41 +08:00
CSRF和ajax有必然联系吗,都说了是Request Forgery,请求肯定是伪造的,要伪造就不一定是通过AJAX这么单纯的一个手段了,什么浏览器的沙箱都挡不了
|
10
ericls 2014-02-22 01:23:38 +08:00
刚刚我也在弄这个
对于ajax(jquery) 需要在ajax前加上 就可 function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } var csrftoken = getCookie('csrftoken'); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } $.ajaxSetup({ crossDomain: false, // obviates need for sameOrigin test beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type)) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); |