Set headers for AJAX request to Django

Don Kirkby

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.

Don Kirkby

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GitHub: How to set headers for ajax request?

From Dev

Ajax cross domain request fail to set headers

From Dev

How set up headers in ajax POST request to include CSRF token

From Dev

Headers not sent with ajax request

From Dev

AJAX request with headers failing

From Dev

How to set common request headers for every ajax request in ext5?

From Dev

How to set common request headers for every ajax request in ext5?

From Java

Angular - Set headers for every request

From Dev

Set headers for scrapy shell request

From Dev

How to set headers for forwarded request

From Dev

How to set request headers in titanium

From Dev

Get raw request headers in django

From Dev

jQuery.ajax() sends request more than once if headers object is set

From Dev

Django - Set value for ajax request in all template files

From Dev

Send Headers in Cross domain request From Ajax

From Dev

What are the Ajax authorization headers for a Bing API request?

From Dev

Custom headers on any ajax request using MooTools

From Dev

setting multiple request headers with jquery ajax

From Dev

Why there are different request headers for same ajax call?

From Dev

ajax request not working in django?

From Dev

ajax request django , jquery

From Dev

Django GET request in AJAX

From Dev

Ajax request with AngularJs and Django

From Dev

Set authorization in Node request.headers

From Dev

Set regular HTTP request headers from javascript

From Dev

Set headers on get request angular 2

From Dev

How to set Rest request Headers parameters in pydocumentdb

From Java

Access Control Request Headers, is added to header in AJAX request with jQuery

From Dev

Flask cookie not set with ajax request

Related Related

HotTag

Archive