Home > Web Front-end > JS Tutorial > body text

How to solve the 403 error when django uses ajax post data

亚连
Release: 2018-05-24 16:53:28
Original
3086 people have browsed it

In Django, when using jquery ajax post data, a 403 error will occur. How to solve it? Let me help you solve the 403 error when using ajax post data in Django. Friends who need it can refer to

This article introduces to you two methods in Django. When using jquery ajax post data, a 403 error will occur. , please see below for details.

Method 1:

If you use jQuery to handle ajax, Django directly sends a piece of code to solve the problem. Put it in a separate js file and introduce it in the html page. Note that this js file must be introduced after the jquery js file is introduced

$(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 = ; i < cookies.length; i++) { 
    var cookie = jQuery.trim(cookies[i]); 
    // Does this cookie string begin with the name we want? 
    if (cookie.substring(, name.length + ) == (name + &#39;=&#39;)) { 
     cookieValue = decodeURIComponent(cookie.substring(name.length + )); 
     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 = &#39;//&#39; + host; 
  var origin = protocol + sr_origin; 
  // Allow absolute or scheme relative URLs to same origin 
  return (url == origin || url.slice(, origin.length + ) == origin + &#39;/&#39;) || 
   (url == sr_origin || url.slice(, sr_origin.length + ) == sr_origin + &#39;/&#39;) || 
   // or any other URL that isn&#39;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(&#39;csrftoken&#39;)); 
 } 
});
Copy after login

Method 2:

Add @csrf_exempt decoration before the view that processes post data Symbols

For example

@csrf_exempt 
def profile_delte(request): 
 del_file=request.POST.get("delete_file",&#39;&#39;)
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

A brief introduction to writing ajax without a library (framework)

Implementing the file upload function based on Ajax and HTML5 in MVC

Jquery and PHP combined to implement AJAX long polling

The above is the detailed content of How to solve the 403 error when django uses ajax post data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!