在django中,使用jquery ajax post数据,会出现403的错误

在django中,使用jquery ajax post数据,会出现403的错误


方法一:


如果用jQuery来处理ajax的话,Django直接送了一段解决问题的代码。把它放在一个独立的js文件中,在html页面中都引入即可。注意这个js文件必须在jquery的js文件引入之后,再引入即可

[javascript] view plain copy print ?
  1. $(document).ajaxSend(function(event, xhr, settings) {  
  2.     function getCookie(name) {  
  3.         var cookieValue = null;  
  4.         if (document.cookie && document.cookie != '') {  
  5.             var cookies = document.cookie.split(';');  
  6.             for (var i = 0; i < cookies.length; i++) {  
  7.                 var cookie = jQuery.trim(cookies[i]);  
  8.                 // Does this cookie string begin with the name we want?   
  9.                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  
  10.                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
  11.                     break;  
  12.                 }  
  13.             }  
  14.         }  
  15.         return cookieValue;  
  16.     }  
  17.     function sameOrigin(url) {  
  18.         // url could be relative or scheme relative or absolute   
  19.         var host = document.location.host; // host + port   
  20.         var protocol = document.location.protocol;  
  21.         var sr_origin = '//' + host;  
  22.         var origin = protocol + sr_origin;  
  23.         // Allow absolute or scheme relative URLs to same origin   
  24.         return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||  
  25.             (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||  
  26.             // or any other URL that isn't scheme relative or absolute i.e relative.   
  27.             !(/^(\/\/|http:|https:).*/.test(url));  
  28.     }  
  29.     function safeMethod(method) {  
  30.         return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));  
  31.     }  
  32.   
  33.     if (!safeMethod(settings.type) && sameOrigin(settings.url)) {  
  34.         xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));  
  35.     }  
  36. });  
$(document).ajaxSend(function(event, xhr, settings) {
    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;
    }
    function sameOrigin(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));
    }
    function safeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});


方法二:

在处理post数据的view前加@csrf_exempt装饰符

例如

  1. @csrf_exempt  
  2. def profile_delte(request):  
  3.     del_file=request.POST.get("delete_file",'')  
  4.       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值