當傳遞X-CSRFToken頭或發送POST數據中的csrfmiddlewaretoken到我的視圖時,它完全跳過了ajax處理程序並直接進入索引/基本視圖。CSRF令牌傳遞中斷我的Django視圖
的Javascript:
$.post("/action/register", {
name: $('#input_name').val(),
email: $('#input_email').val(),
csrfmiddlewaretoken: '{{ csrf_token }}'
}, function(data) {
var response;
try {
response = JSON.parse(data.toString());
} catch(e) {
response = {"status":"error", "message":"Please try again later, we're having some tech issues!"};
}
// ... process some stuff, the callback works which is good
查看:
def handle_register(req):
''' Request handler pyfor registration, should return JSON for the AJAX request. '''
if req.is_ajax():
return validate_and_register(req)
else:
return HttpResponse('{"status":"error","message":"Invalid request."}')
urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^action/register$', 'views.handle_register', name='handle_register'),
url(r'^admin/', include(admin.site.urls)),
url(r'^.*$', 'views.handle_home', name='handle_home'),
)
當我不及格頭,我得到一個403(CSRF_VERIFICATION_FAILED)。當我傳遞適當的頭文件(在多個瀏覽器中確認)時,它會跳過/ action/register處理程序並直接進入「handle_home」視圖。
我被卡住了!
此外,/管理/也不管用,它只是加載handle_home視圖。 – Kyle