Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax的CSRF问题,模板中没有表单_Javascript_Jquery_Python_Ajax_Django - Fatal编程技术网

Javascript ajax的CSRF问题,模板中没有表单

Javascript ajax的CSRF问题,模板中没有表单,javascript,jquery,python,ajax,django,Javascript,Jquery,Python,Ajax,Django,当我尝试将POST与ajax一起使用时,我当前的权限被拒绝。 我相信这是因为CSRF,因为当我使用 @csrf\u豁免我认为是装饰者。如果有人能告诉我我在这里可能做错了什么,我将不胜感激。我试着发布了一篇文章,但是没有帮助。然后我尝试遵循python文档中关于这个问题的内容,但是我仍然得到了“权限被拒绝”错误。 这是我的密码 在我看来,我正在做这样的事情 @csrf_protect def showMgmt(request): cntxt = {} ..... .....

当我尝试将POST与ajax一起使用时,我当前的权限被拒绝。 我相信这是因为CSRF,因为当我使用
@csrf\u豁免
我认为是装饰者。如果有人能告诉我我在这里可能做错了什么,我将不胜感激。我试着发布了一篇文章,但是没有帮助。然后我尝试遵循python文档中关于这个问题的内容,但是我仍然得到了“权限被拒绝”错误。 这是我的密码

在我看来,我正在做这样的事情

@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请求尝试在另一个函数上执行POST事件
AjaxDestination

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); 
                        }
         });
        }
page:management.html
//使用jQuery
函数getCookie(名称){
var-cookieValue=null;
if(document.cookie&&document.cookie!=''){
var cookies=document.cookie.split(“;”);
对于(变量i=0;i

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

我认为没有必要使用语句
var token=$('input[name=“csrfmiddlewaretoken”]”)。prop('value')
还有,您是否尝试将
csrfmiddlewaretoken
放在POST数据中而不是请求头中?我认为不需要语句
var token=$('input[name=“csrfmiddlewaretoken”]')。prop('value')还有,您是否尝试将
csrfmiddlewaretoken
放在POST数据中而不是请求头中?