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

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

问题描述

我有两个AJAX调用从jQuery的Web服务。

第一个调用(的getMessages )开始在JavaScript的时间间隔(的setInterval ),并返回消息的字符串数组存储在一个会话变量。

第二个电话( UploadUsers )上传的用户,并保存在session的状态,在方法返回的getMessages 。所以UploadUsers增加了消息的会话和的getMessages检索信息并显示它们的客户端。

现在的问题是,即使我呼吁这两种方法异步,的getMessages 等待,直到 UploadUsers 结束。它只是加载。

我甚至把每个用户之间的Thread.sleep代码被添加,我希望有的getMessages 1/10用户已添加,2/10回报用户已被添加,在每一个单独调用。

会发生什么事是的getMessages 不返回任何东西,直到 UploadUsers 结束,然后把所有的文字一次

我有很多的code,所以我不知道放什么,但这里有云:

UploadUsers.aspx

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

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

Site.js

 函数callAsynchMethod(URL,的keyValue回调,callBackError){
    $阿贾克斯({
        键入:POST,
        网址:网址,
        数据:的keyValue,
        的contentType:应用/ JSON的;字符集= UTF-8,
        成功:回调,
        错误:callBackError
    });
}
 

ClientStatusHandler.asmx.cs

 常量字符串键=LUMessages;
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)
        [WebMethod的(EnableSession =真)
        公共字符串[]的getMessages()
        {

            如果(会话[关键]!= NULL)
            {
                字符串[]消息=((IList的<字符串>)会议[关键])的ToArray()。
                ((IList的<字符串>)会议[关键])。清除();
                回短信;
            }
            返回新的字符串[] {};
        }







  [ScriptMethod(ResponseFormat = ResponseFormat.Json)
     [WebMethod的(EnableSession =真)
      公共字符串[] UploadUsers()
     {
    //这个函数添加一个用户,并呼吁SaveMessageToQueue
//为每个用户。
    //我看到被输入到会话中的消息,
//我调用了Thread.Sleep(10000)
    //消息之间。
    }









     私人无效SaveMessageToQueue(串M)
        {

        IList的<字符串> messageQueue =(名单<字符串>)HttpContext.Current。
会话[LUMessages];
        messageQueue.Add(米);
         }
 

解决方案

这是因为你必须使会议和会议锁定所有的通话,直到他们回来了。

您还可以阅读这个问题:更换ASP.Net的会议完全

还阅读: http://stackoverflow.com/a/3660837/159270

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

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

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.

The problem is even though I call both methods asynchronously, GetMessages waits until UploadUsers is finished. 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.

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:

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

解决方案

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

also read: http://stackoverflow.com/a/3660837/159270

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

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