Javascript 多响应 AJAX 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6475238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Response AJAX request
提问by Peter Olson
Is there a way to have one AJAX request with multiple responses?
有没有办法让一个 AJAX 请求有多个响应?
For example, if make a GET request to the server which will take a long time to calculate, how could I have the server occasionally send back responses which give me some data about the progress?
例如,如果向服务器发出一个需要很长时间计算的 GET 请求,我怎么能让服务器偶尔发回一些关于进度的数据的响应?
If so, could somebody post an example, preferably with Jquery and an explanation of the mechanism through which the server can does it?
如果是这样,有人可以发布一个示例,最好使用 Jquery 并解释服务器可以通过的机制吗?
采纳答案by The Scrum Meister
You can implement this using 2 ajax calls, one to run the process and a second call to periodically poll the progress:
您可以使用 2 个 ajax 调用来实现这一点,一个是运行进程,另一个是定期轮询进度:
On the server side:
在服务器端:
public class ProgressInfo
{
public int Percent {get;set;}
public bool Done {get;set;}
}
public JsonResult DoCalculation(string id)
{
ProgressInfo progress = new ProgressInfo();
if(!string.IsNullOrEmpty(id))
{
Session[id] = progress;
}
//periodicly update progress
progress.Percent++;
}
public JsonResult GetProgress(string id)
{
ProgressInfo progress;
if(string.IsNullOrEmpty(id)
|| (progress = Session[id] as ProgressInfo) == null)
{
return Json(new {
success = false
});
}
if(progress.done)
{
Session.Remove(id);
}
return Json(new {
success = true,
done = progress.done,
percent = progress.Percent
});
}
On the client side:
在客户端:
var progressID = Math.random();
function doCalculation() {
$.post('<%=Url.Action("DoCalcluation")%>/' + progressID);
setTimeout(pollProgress, 1000);
}
function pollProgress() {
$.post('<%=Url.Action("GetProgress")%>/' + progressID, function(response){
if(!response.success) {
alert('Cannot find progress');
return;
}
if(response.done) {
alert('Done!');
} else {
alert('Progress at ' + response.precent + '%');
setTimeout(pollProgress, 1000 /*1 second*/);
}
}, 'json');
}
回答by genesis
Fast answer: No, it isn't possible.
快速回答:不,这是不可能的。
You should send more requests to get more responses
您应该发送更多请求以获得更多响应
回答by vlscanner
You most likely need help from the server side coding because looks like you need reverse ajax or what is also called comet push. I do not know which language you use on server side but the basic idea is to delay http response for as long as browser will allow using endless loop (on server side) and push data while connection is alive
您很可能需要服务器端编码的帮助,因为看起来您需要反向 ajax 或也称为彗星推送。我不知道您在服务器端使用哪种语言,但基本思想是延迟 http 响应,只要浏览器允许使用无限循环(在服务器端)并在连接有效时推送数据
You may want to check this out: http://code.google.com/p/google-web-toolkit-incubator/wiki/ServerPushFAQ
你可能想看看这个:http: //code.google.com/p/google-web-toolkit-incubator/wiki/ServerPushFAQ
回答by Slakkie
Have a look at xajax it can send multiple responses back, and one can ever redirect them to certain areas. In this regard xajax is much better than jQuery. I can make one request and the server can compose a response which contains multiple responses either HTML, JavaScript, calls, etc. You can even say put this there and that there with options like append, replace etc. very cool. jQuery should do this then I can switch once and for all to jQuery... This is the only thing that is keeping me from switching over.
看看 xajax 它可以发送多个响应,并且可以将它们重定向到某些区域。在这方面,xajax 比 jQuery 好得多。我可以发出一个请求,服务器可以编写一个响应,其中包含 HTML、JavaScript、调用等多个响应。你甚至可以说把它放在那里,那里有附加、替换等选项,非常酷。jQuery 应该这样做,然后我可以一劳永逸地切换到 jQuery...这是唯一阻止我切换的事情。
回答by Nirbhay Mishra
One Ajax request ----------->You get One response.
一个 Ajax 请求 ----------->您得到一个响应。
What you can actually do is.
你实际上可以做的是。
check the response data and do DIFFERENT action as per the data fetched.
检查响应数据并根据获取的数据执行不同的操作。