长时间运行的jQuery的Ajax调用 [英] Long running jQuery Ajax call

查看:201
本文介绍了长时间运行的jQuery的Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jQuery的AJAX调用从WEB API服务摘要计数哪个请求。在WEB API处理请求并调用传递请求参数的存储过程(SQL Server)的。存储过程需要较长时间(超过10分钟来处理请求。如果存储过程需要10多分钟jQuery的AJAX调用正在报告与状态code 12002或12152未知错误(这些错误涉及到连接问题),但在Web服务器10分钟后收到的结果从存储过程了。问题是在浏览器中存在的是IE8.0,IE9.0,火狐但铬接收,即使它等待超过10分钟的反应。有没有什么解决方案,以保持通讯的服务器和客户端之间还活着。我试图从阿贾克斯改变连接请求标题为关闭,开放,保持活跃没有什么作品。请帮助我。

Java脚本code。

  $。阿贾克斯({
类型:后,
网址:网址preFIX +'API / querydispatcher /摘要,
数据:S,
成功:功能(数据){
window.clearInterval(INT);
$('#等待')隐藏()。
$('#CancelSummary')隐藏()。
$('#backgroundmodal')隐藏()。
$(#tResultTotals)了slideDown('慢')。
DeserializeJSon(EVAL(数据));
     如果(!CancelSummaryRequest){
           CancelSummaryRequest = TRUE;
          $('#DownloadDetailedReport)显示()。
    }
            其他{                $('#tResultTotals')隐藏()。
            }
        },
            $('#等待')隐藏()。
            $('#CancelSummary')隐藏()。
            $('#backgroundmodal')隐藏()。
            ERROR_DIALOG(request.responseText);
        }    });
}服务器端(WEB API)code。  [WebInvoke(UriTemplate =摘要,方法=POST)]
        公开名单<&QueryResult中GT; GetSummaryReport_Queue(JsonValue SummaryXML)
        {
            MyContext DB =新MyContext();
            DateTime的开始时间= DateTime.Now;            。字符串sumXML = SummaryXML [XMLJson]的ToString()更换(@\\,​​).Replace(@,)。
            //这是两个$ C $ @ CS需要被剪掉字符串的结尾创建由JSON。
            sumXML = sumXML.Replace(u000du000a,);            清单<&QueryResult中GT;结果= NULL;            的XElement xxml = XElement.Parse(sumXML);
            字符串ReportID = xxml.Descendants(ReportId)FirstOrDefault()值。;            字符串ERR =;
            尝试
            {                结果= db.fnReportResult(sumXML).ToList();
             }
            赶上(例外五)
            {                ERR = e.Message +:+(e.InnerException =空e.InnerException.Message!?);
                Ë扔掉;
            }
            最后{
                /// ---记录审计信息。
                双RunningTime = DateTime.Now.Subtract(开始时间).TotalMilliseconds;
                字符串参数=ApplicationType:Query_Dispatcher+||用户:。+ SummaryXML [用户ID]的ToString()更换(@\\,​​).Replace(@,)+
                        ||会话:+ SummaryXML [会话]的ToString()更换(@\\,​​).Replace(@,)+。
                        ||运行时间:+ RunningTime.ToString()+||请求类型:总结报告+
                        ||报告ID:+ ReportID +
                        ||错误:+走错了路。                审计SaveAudit =新的审计();
                SaveAudit.WriteAudit(Query_Builder参数);
                // #### - 记录审计信息
            }            返回结果;
        }


解决方案

浏览器内置的,可能影​​响这个内部超时。关于它的更多信息请参阅本StackOverflow的问题:浏览器超时

正如其他人所说,等待10分钟,Ajax响应是非常糟糕的,和大多数浏览器将可能一次出其默认设置 - 即使你设定的AJAX超时高得离谱。我怀疑你将永远得到这个工作。

正如其他人说你可以做和运行查询,然后向用户发送链接的结果。然而,另一种解决方案将是上一个cron运行查询每隔x分钟并缓存的结果。这种方式,用户可以查看关于需求的结果,而不必等待一个新的URL或等待的长时间

I have jQuery AJAX call which request for summary count from WEB API Services. The WEB API process the request and calls a stored procedure (sql server) passing the request parameters. The stored procedure takes longer time (more than 10 minutes to process the request. If the Stored Procedure takes more than 10 minutes the jQuery AJAX call is reporting an unknown error with status code 12002 or 12152(these errors relates to connectivity issue) but the Web Server receives the result back from the stored procedure after 10 minutes. The problem is occuring on browser is IE8.0 , IE9.0, firefox but chrome is receiving the response even if it wait beyond 10 minutes. Is there any solutions to keep the communication alive between the server and the client. I have tried changing the connection request header from ajax to "close","open","keep-alive" nothing works. Pls help me out.

Java Script Code.

$.ajax({
type: "post",
url: URLPrefix + 'api/querydispatcher/summary',
data: s,
success: function (data) {
window.clearInterval(int);
$('#wait').hide();
$('#CancelSummary').hide();
$('#backgroundmodal').hide();
$("#tResultTotals").slideDown('slow');
DeserializeJSon(eval(data));
     if (!CancelSummaryRequest) {
           CancelSummaryRequest = true;
          $('#DownloadDetailedReport').show();
    }
            else {

                $('#tResultTotals').hide();
            }
        },
            $('#wait').hide();
            $('#CancelSummary').hide();
            $('#backgroundmodal').hide();
            error_Dialog(request.responseText);
        }

    });
}

Server Side (WEB API) code.

  [WebInvoke(UriTemplate = "summary", Method = "POST")]
        public List<QueryResult> GetSummaryReport_Queue(JsonValue SummaryXML)
        {
            MyContext db = new MyContext();
            DateTime StartTime = DateTime.Now;

            string sumXML = SummaryXML["XMLJson"].ToString().Replace(@"\", "").Replace(@"""", "");
            //These are two codes created by JSon @ the end of the String that need to be trim off.
            sumXML = sumXML.Replace("u000du000a", "");

            List<QueryResult>  results = null;

            XElement xxml = XElement.Parse(sumXML);
            string ReportID = xxml.Descendants("ReportId").FirstOrDefault().Value;

            string err = "";
            try
            {

                results = db.fnReportResult(sumXML).ToList();
             }
            catch (Exception e)
            {

                err = e.Message + " : "+(e.InnerException!=null?e.InnerException.Message : "");
                throw e;
            }
            finally {
                ///--- Record Audit Info.
                double RunningTime = DateTime.Now.Subtract(StartTime).TotalMilliseconds;
                string parameters = "ApplicationType:Query_Dispatcher" + "||User:" + SummaryXML["UserId"].ToString().Replace(@"\", "").Replace(@"""", "") +
                        "||Session:" + SummaryXML["Session"].ToString().Replace(@"\", "").Replace(@"""", "") +
                        "||Running Time:" + RunningTime.ToString() + "||Request Type:Summary Report" +
                        "||Report ID:" + ReportID +
                        "||Error:" + err;

                Audit SaveAudit = new Audit();
                SaveAudit.WriteAudit("Query_Builder", parameters);
                //####-Recording Audit Info
            }

            return results;
        }

解决方案

Browsers have built in, internal timeouts that may be effecting this. See this StackOverflow question for more information on it: Browser Timeouts

As others have said, waiting 10 minutes for an AJAX response is very bad, and most browsers will probably time you out in their default settings - even if you set the AJAX timeout absurdly high. I doubt you will ever get this to work.

You could do as others have said and run the query then send users a link to the results. However, another solution would be to run the query on a cron every x minutes and cache the results. This way, users can view the results on demand and not have to wait for a new url or wait a prolonged period of time.

这篇关于长时间运行的jQuery的Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆