带有ajax的CSRF问题,模板中没有任何形式

时间:2017-03-21 08:29:59

标签: javascript jquery python ajax django

当我尝试使用带有ajax的POST时,我正在获得权限被拒绝。 我相信这是因为CSRF因为我的帖子在我使用时很好 我的观点是@csrf_exempt装饰者。我很感激,如果有人能告诉我这里可能做错了什么。我试过this但是这样做没有帮助。我接着尝试按照python文档here关于这个问题但是我我仍然得到权限被拒绝错误。 这是我的代码

在视图中我正在做这样的事情

@csrf_protect
def showMgmt(request):
    cntxt = {}
    .....
    .....
    response = render(request, 'management.html', cntxt)
    return response

@csrf_protect
def AjaxDestination(request):
    return response("...")

现在,首先showMgmt函数显示management.html,其中包含以下ajax请求。此ajax请求尝试在另一个函数AjaxDestination

上执行POST事件
page:management.html

<script>
// using jQuery
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));
}
function sameOrigin(url) {
    // test that a given url is a same-origin URL
    // url could be relative or scheme relative or absolute
    var host = document.location.host; // host + port
    var protocol = document.location.protocol;
    var sr_origin = '//' + host;
    var origin = protocol + sr_origin;
    // Allow absolute or scheme relative URLs to same origin
    return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
        (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
        // or any other URL that isn't scheme relative or absolute i.e relative.
        !(/^(\/\/|http:|https:).*/.test(url));
}

$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
            // Send the token to same-origin, relative URLs only.
            // Send the token only if the method warrants CSRF protection
            // Using the CSRFToken value acquired earlier
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});

function jsonResult_ajaxCall(url,data,callback){
              $.ajax({
                  type: 'POST',
                  url: url,
                  dataType: "text",
                  data : data,

                  success: function(response) {
                         var jresult = JSON.parse(response);
                            callback(jresult);
                        },
                  error: function(xhr) {
                            callback(false); 
                        }
         });
        }

函数jsonResult_ajaxCall基本上调用了ajax函数。 关于为什么我仍然被拒绝许可的任何建议都会有所帮助。感谢

0 个答案:

没有答案