对 Web 服务的 jQuery Ajax 调用似乎是同步的 [英] jQuery Ajax calls to web service seem to be synchronous

查看:29
本文介绍了对 Web 服务的 jQuery Ajax 调用似乎是同步的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两次从 jquery 调用 web 服务的 ajax 调用.

I have two ajax calls to a web service from jquery.

第一次调用 (GetMessages) 在 javascript (setInterval) 中开始一个间隔,并返回存储在会话变量中的消息字符串数组.

The first call (GetMessages) starts an interval in javascript (setInterval) and returns a string array of messages stored in a session variable.

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

The second call (UploadUsers) uploads users and saves the status in the session to be returned in the method GetMessages. So UploadUsers adds messages to the Session and GetMessages retrieves the messages and displays them for the client.

问题是即使我异步调用这两种方法,GetMessages 也会等到 UploadUsers 完成.它只是加载.

The problem is even though I call both methods asynchronously, GetMessages waits until UploadUsers is finished. It just loads.

我什至在添加的每个用户之间放置了一个 thread.sleep 并且我希望 GetMessages 返回已添加 1/10 个用户"、已添加 2/10 个用户",每个单独通话.

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.

发生的情况是 GetMessagesUploadUsers 完成之前不会返回任何内容,然后立即带来所有文本.

What happens is GetMessages doesn't return anything until UploadUsers is finished and then brings all the text at once.

我有很多代码,所以我不知道该放什么,但是这里是:

I have a lot of code so I don't know what to put but here it goes:

上传Users.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);
         }

推荐答案

这是因为您启用了 Session 并且 Session 锁定了所有调用,直到它们返回为止.

This is because you have enable the Session and the Session lock all the calls until they return.

您也可以阅读这个问题:完全替换 ASP.Net 的会话

You can read also this question: Replacing ASP.Net's session entirely

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

这篇关于对 Web 服务的 jQuery Ajax 调用似乎是同步的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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