我有两个来自 jquery 对 Web 服务的 ajax 调用。
第一次通话(GetMessages
) 在 javascript 中开始一个间隔 (setInterval
)并返回存储在会话变量中的消息字符串数组。
第二次调用(UploadUsers
) 上传用户并将状态保存在要在方法中返回的会话中GetMessages
。因此,UploadUsers 将消息添加到会话中,而 GetMessages 检索消息并将其显示给客户端。
问题是即使我异步调用这两个方法,GetMessages
等到UploadUsers
完成了。它只是加载。
我什至在添加的每个用户之间放置了一个 thread.sleep ,我希望有GetMessages
返回“已添加 1/10 个用户”、“已添加 2/10 个用户”,每个都在单独的调用中。
发生的情况是GetMessages
不返回任何内容,直到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);
}