Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do an AJAX request with an authenticated user in Django?

Tags:

jquery

django

I need to do an HTTP call using AJAX with an already authenticated user using Django.

Is there any standard simple way to do this? I've read this other question but uses a different technology.

I'm using HTTP (not HTTPS) and using a third-party app like django-tastypie for this unique "simple" purpose would be overkill, wouldn't it?

like image 225
Caumons Avatar asked Aug 24 '13 02:08

Caumons


People also ask

How can I send authorization token in AJAX?

To send a GET request with a Bearer Token authorization header using JavaScript/AJAX, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

How send data from AJAX to Django?

To send and receive data to and from a web server, AJAX uses the following steps: Create an XMLHttpRequest object. Use the XMLHttpRequest object to exchange data asynchronously between the client and the server. Use JavaScript and the DOM to process the data.


1 Answers

Ajax requests are no different from any other kind of request. If the user is already authenticated, then the Ajax request will be authenticated too: the session cookie which identifies the user will be sent along with the Ajax request just as it would be with any other one.

(That linked question is irrelevant because it is talking about authenticating the user within the request, not using an already-authenticated user as you want.)

like image 80
Daniel Roseman Avatar answered Oct 23 '22 08:10

Daniel Roseman