jQuery Ajax调用Web服务似乎是同步的

我有两个来自jQuery的Web服务的Ajax调用。

第一个调用( GetMessages )在javascript( setInterval )中启动一个时间间隔,并返回存储在会话variables中的string数组。

第二个调用( UploadUsers )上传用户并保存会话中的状态,以便在方法GetMessages返回。 因此,UploadUsers会将消息添加到会话中,GetMessages会检索消息并将其显示给客户端。

问题是即使我asynchronous调用两个方法, GetMessages等待,直到UploadUsers完成。 它只是加载。

我甚至把每个用户添加一个thread.sleep,我希望有GetMessages返回“1/10用户已被添加”,“2/10用户已被添加”,每个在不同的调用。

GetMessages不会返回任何东西,直到UploadUsers完成,然后一次带来所有的文本。

我有很多的代码,所以我不知道该怎么做,但在这里:

UploadUsers.aspx

 callAsynchMethod('ClientStatusHandler.asmx/GetMessages', '', printMessages, stopPollingError); callAsynchMethod('ClientStatusHandler.asmx/StartRetrievingLeads', data, stopPolling, stopPollingError); 

Site.js

 function callAsynchMethod(url, keyValue, callBack, callBackError) { $.ajax({ type: "POST", url: url, data: keyValue, contentType: "application/json; charset=utf-8", success: callBack, error:callBackError }); } 

ClientStatusHandler.asmx.cs

 const string key = "LUMessages"; [ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod(EnableSession = true)] public string[] GetMessages() { if (Session[key] != null) { string[] messages = ((IList<string>)Session[key]).ToArray(); ((IList<string>)Session[key]).Clear(); return messages; } return new string[] { }; } [ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod(EnableSession = true)] public string[] UploadUsers() { //This function adds a user and calls SaveMessageToQueue //for every user. //I see the message being entered into the session and //I call Thread.Sleep(10000) //between messages. } private void SaveMessageToQueue(string m) { IList<string> messageQueue = (List<string>)HttpContext.Current. Session["LUMessages"]; messageQueue.Add(m); } 

这是因为您已启用会话和会话locking所有的电话,直到他们返回。

你也可以阅读这个问题: 完全取代ASP.Net的会话

也阅读: https : //stackoverflow.com/a/3660837/159270