I want to test some AJAX requests to my Django site, but the server doesn't think the request from my test tool is an AJAX request. What HTTP header do I need to set?
The server code has a test like this:
# in myapp/ajax.py
def my_request(request, some_id):
if request.is_ajax():
return json.dumps([some_id, 'processed for AJAX'])
# some other processing, or an error
I'm using Postman to send my AJAX request, and I've learned to set the Accept
header to application/json
, but what header do I set to show that it's an AJAX request?
In the Django code, I found the test method:
def is_ajax(self):
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
When I set the HTTP_X_REQUESTED_WITH
header to XMLHttpRequest
, the test still fails.
When I turned on the Chrome developer tools, I saw that a regular AJAX request includes the header X-Requested-With
set to XMLHttpRequest
. Adding that header in the Postman request makes it work. It's just a slightly different name.
If you are making AJAX requests that modify data, you'll want to switch to making POST requests. That brings Django's cross-site request forgery tools into play, so you'll need to copy your CSRF token from some other form's hidden field, and then paste it in the X-CSRFToken
header when you test your AJAX requests.
Anytime I'm sending Ajax data via Django I always put this at the top.
// django ajax request info
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);
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.