主頁 > 知識(shí)庫(kù) > django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)

django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)

熱門標(biāo)簽:科智聯(lián)智能電銷機(jī)器人 上海浦東百度地圖標(biāo)注中心注冊(cè) 百靈鳥 襄陽外呼系統(tǒng)接口 青海醫(yī)療智能外呼系統(tǒng)怎么樣 目標(biāo)三維地圖標(biāo)注 外呼系統(tǒng)獲取客戶手機(jī)號(hào) 徐州電銷卡外呼系統(tǒng)供應(yīng)商 老虎郵局地圖標(biāo)注點(diǎn)

在django中,使用jquery ajax post數(shù)據(jù),會(huì)出現(xiàn)403的錯(cuò)誤

方法一:

如果用jQuery來處理ajax的話,Django直接送了一段解決問題的代碼。把它放在一個(gè)獨(dú)立的js文件中,在html頁面中都引入即可。注意這個(gè)js文件必須在jquery的js文件引入之后,再引入即可

$(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數(shù)據(jù)的view前加@csrf_exempt裝飾符

例如

@csrf_exempt 
def profile_delte(request): 
  del_file=request.POST.get("delete_file",'')

以上通過兩種方法解決了django ajax post 403錯(cuò)誤,當(dāng)然解決方法也不止這兩種,歡迎多多分享自己的見解,本文寫的不好,還請(qǐng)見諒,謝謝。

您可能感興趣的文章:
  • Django框架如何使用ajax的post方法
  • django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤如何解決
  • Django中ajax發(fā)送post請(qǐng)求 報(bào)403錯(cuò)誤CSRF驗(yàn)證失敗解決方案
  • django ajax發(fā)送post請(qǐng)求的兩種方法
  • django獲取ajax的post復(fù)雜對(duì)象的實(shí)現(xiàn)方法
  • django學(xué)習(xí)之a(chǎn)jax post傳參的2種格式實(shí)例

標(biāo)簽:佛山 商洛 荊州 股票 辛集 揭陽 咸寧 紅河

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)》,本文關(guān)鍵詞  django,中,使用,jquery,ajax,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)》相關(guān)的同類信息!
  • 本頁收集關(guān)于django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章