简体   繁体   English

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

[英]jQuery Ajax calls to web service seem to be synchronous

I have two ajax calls to a web service from jquery. 我有两个从jquery到Web服务的ajax调用。

The first call ( GetMessages ) starts an interval in javascript ( setInterval ) and returns a string array of messages stored in a session variable. 第一次调用( GetMessages )在javascript( setInterval )中启动一个间隔,并返回存储在会话变量中的消息的字符串数组。

The second call ( UploadUsers ) uploads users and saves the status in the session to be returned in the method GetMessages . 第二个调用( UploadUsers )上传用户并将状态保存在要返回的会话中,方法为GetMessages So UploadUsers adds messages to the Session and GetMessages retrieves the messages and displays them for the client. 因此,UploadUsers将消息添加到Session,而GetMessages检索消息并将其显示给客户端。

The problem is even though I call both methods asynchronously, GetMessages waits until UploadUsers is finished. 问题是,即使我异步调用了这两种方法, GetMessages UploadUsers等到UploadUsers完成。 It just loads. 它只是加载。

I even put a thread.sleep between each user being added and I expect to have GetMessages return "1/10 users have been added", "2/10 users have been added", each on separate calls. 我什至在要添加的每个用户之间放置一个thread.sleep,并且我希望GetMessages返回“已添加1/10个用户”,“已添加2/10个用户”,每个都分别调用。

What happens is GetMessages doesn't return anything until UploadUsers is finished and then brings all the text at once. 发生的事情是,直到UploadUsers完成, GetMessages才返回任何内容,然后UploadUsers所有文本。

I have a lot of code so I don't know what to put but here it goes: 我有很多代码,所以我不知道要放什么,但是事情就这样了:

UploadUsers.aspx UploadUsers.aspx

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

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

Site.js 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 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);
         }

This is because you have enable the Session and the Session lock all the calls until they return. 这是因为您已启用会话,并且会话锁定了所有呼叫,直到它们返回。

You can read also this question: Replacing ASP.Net's session entirely 您还可以阅读以下问题: 完全替换ASP.Net的会话

also read: https://stackoverflow.com/a/3660837/159270 另请阅读: https : //stackoverflow.com/a/3660837/159270

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM