简体   繁体   中英

Respond to AJAX request at a later time

Is it possible with AJAX and Django as the server to have a user's JS make a AJAX request and then have Django respond with what the user requested at a much later time, ie when the server has what the user's JS wants?

My idea:

-AJAX requests for object

-Django caches request

-When object that user wants it available, Django signal tells
     view to respond to that request with the object

However, I don't know how to allow the JS script to continue on doing other things and then attend to the server's response when it gets it, or to have the AJAX NOT count the server's not responding with the object (or at all) it wants as an error. Is this possible WITHOUT having the JS infinitely loop AJAX requests until it gets what it wants? Could this be done with Django just giving the user the objects when it has them without the AJAX requesting them all?

I suppose you are looking for heavy backend processing job which sends results when processing is complete. So for that you can opt for any of the below mentioned process

  1. Long polling - here you can do periodic ajax calls to check if the processing is complete and once its complete you can show the result and stop the periodic ajax checks.

  2. Web sockets - this is better approach as per me. django-websocket-redis is the library which I used to achieve the same. The library has good enough documentation to get you started.

Hope this helps you. :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM