2014-07-09 2 views
1

CSRF 받기 403 아래의 console.log 문은 토큰을 점유하고 있음을 확인합니다. 내 로컬 서버의 동일한 도메인에 요청을 제출하고 있습니다.Django CSRF 403

internal.csrfToken = $.cookie('csrftoken'); 

    internal.csrfSafeMethod = function(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) { 
     console.log("ajaxSetup"); 
     console.log(internal.csrfToken); 
     if (!internal.csrfSafeMethod(settings.type)) { 
     console.log("Settings type"); 
     xhr.setRequestHeader("X-CSRFToken", internal.csrftoken); 
     } 
    } 
    }); 

    external.submitPayment = function (app_id, charge_now_amount, stripe_plan_id) { 
    // Submit a payment to the server and handle any errors. 

    $.ajax({ 
     url: URLS.postPayment, 
     type: 'POST', 
     data: { 
     'app_id': STRIPE_CONFIG.app.id, 
     'amount': charge_now_amount, 
     'stripe_plan_id': stripe_plan_id 
     }, 
     dataType: 'json', 
     success: function(response) { 
     alert("Success!"); 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
     alert("Error!"); 
     } 
    }); 

    }; 

답변

0

이 방법이 도움이 될지 확실하지 않습니다. 나는 비슷한 문제가 있었다. 그리고 X-CSRFToken을 추가 한 beforeSend 함수를 만들어서 수정했습니다.

$.ajax({ 
    url: url, 
    data: JSON.stringify({'name': value }), 
    type: 'POST', 
    dataType: 'json', 
    beforeSend: function (jqXHR, settings) { 
    jqXHR.setRequestHeader('X-CSRFToken', $('input[name=csrfmiddlewaretoken]').val()); 
    }, 
    success: function(response) { 
    alert("Success!"); 
    } 
})