python - Set headers for AJAX request to Django -
i want test ajax requests django site, server doesn't think request test tool ajax request. http header need set?
the server code has test this:
# in myapp/ajax.py def my_request(request, some_id): if request.is_ajax(): return json.dumps([some_id, 'processed ajax']) # other processing, or error i'm using postman send ajax request, , i've learned set accept header application/json, header set show it's ajax request?
in django code, found test method:
def is_ajax(self): return self.meta.get('http_x_requested_with') == 'xmlhttprequest' when set http_x_requested_with header xmlhttprequest, test still fails.
when turned on chrome developer tools, saw regular ajax request includes header x-requested-with set xmlhttprequest. adding header in postman request makes work. it's different name.
if making ajax requests modify data, you'll want switch making post requests. brings django's cross-site request forgery tools play, you'll need copy csrf token other form's hidden field, , paste in x-csrftoken header when test ajax requests.
Comments
Post a Comment