栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

多重回应AJAX要求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多重回应AJAX要求

您可以使用2个ajax调用来实现此目的,一个调用运行该进程,第二个调用定期轮询进度:

在服务器端:

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    });}

在客户端:

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 ); }    }, 'json');}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406728.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号