简体   繁体   English

稍后回复AJAX请求

[英]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? 使用AJAX和Django作为服务器,是否可以让用户的JS发出AJAX请求,然后让Django在更晚的时间响应用户的请求,即服务器何时满足用户的JS要求?

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. 但是,我不知道如何让JS脚本继续做其他事情,然后在获得脚本时处理服务器的响应,或者让AJAX不计算服务器对对象的响应(或根本没有响应)它想作为一个错误。 Is this possible WITHOUT having the JS infinitely loop AJAX requests until it gets what it wants? 如果不让JS无限循环AJAX请求,直到获得所需的结果,这是否可能? Could this be done with Django just giving the user the objects when it has them without the AJAX requesting them all? 可以用Django在拥有对象时仅向用户提供对象而无需AJAX要求全部对象来完成吗?

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. 长轮询-在这里您可以进行定期Ajax调用以检查处理是否完成,一旦完成,您可以显示结果并停止定期Ajax检查。

  2. Web sockets - this is better approach as per me. Web套接字-按照我的说法,这是更好的方法。 django-websocket-redis is the library which I used to achieve the same. django-websocket-redis是我用来实现相同目的的库。 The library has good enough documentation to get you started. 该库具有足够好的文档,可以帮助您入门。

Hope this helps you. 希望这对您有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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