Long running jQuery Ajax call

2019-09-09 06:09发布

问题:

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

回答1:

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.