Ajax异步发布


                    

我正在尝试制作如下所示的httpost。 我希望js从每5秒调用一次的ajax下独立工作。 但这不是异步的。 它表现为同步。 它在ajax期间阻止js。 你能帮我理解这个问题吗?</ p>

  $。ajax({
             输入:“ POST”,
             contentType:“ application / json; charset = utf-8”,
             网址:“ ../ Home / GetMobiles”,
             数据:JSON.stringify(model),
             dataType:“ json”,
             异步:是的,
             成功:功能(数据)
             {
                 DivLd.style.display =“ none”;
                 SetMoving(数据);
                 glob =数据;
             },
             错误:function(xhr,err){
                 GetToken();
             }
         });
</ code> </ pre>
     </ div>

展开原文

原文

I'm trying to make an httpost like below. I want the js to work independent from below ajax which is called per 5 seconds. But it's not working async. It behaves as sync. It blocks the js during ajax. Can you please help me to understand the problem ?

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "../Home/GetMobiles",
            data: JSON.stringify(model),
            dataType: "json",
            async: true,
            success: function(data) 
            {
                DivLd.style.display = "none";
                SetMoving(data);
                glob = data;
            },
            error: function(xhr, err) {
                GetToken();
            }
        });

1个回答


我认为在这种情况下无法实现所需的功能,因为在AJAX请求之后运行的JS代码取决于AJAX响应数据。 在AJAX请求完成之前,您无法运行JS代码。 即使请求本身与其余的JavaScript代码“异步”(我用异步引号括起来,因为JS通常不是异步的-可能看起来像是异步的,但实际上有一个处理所有JS操作的后台队列) ,将JS代码移出AJAX响应代码将不起作用。 可以回答您的问题吗?</ p>

现在,话虽这么说,有时您可能希望在发出AJAX请求之前立即执行某些操作,例如显示“正在加载...”屏幕或类似内容。 如果您想在不等待响应数据的情况下进行某些操作,并且这些操作不依赖于该数据,则可以在$ .ajax调用之前添加它们。</ p>
     </ div>

展开原文

原文

I think in this case it is not possible to achieve what you're looking for since the JS code that runs after the AJAX request depends on the AJAX response data. You can't have the JS code run until the AJAX request is completed. Even though the request itself is 'asynchronous' to the rest of the JavaScript code (I put asynchronous in quotes because JS in general is not asynchronous -- it may appear to be, but really there is a background queue which processes all JS operations), moving the JS code out of the AJAX response code will not work. Does that answer your question?

Now, having said that, sometimes you might want to run certain operations right before you issue the AJAX request, such as showing a "Loading..." screen or similar. If there are operations you want to happen without waiting for the response data and those operations don't depend on that data, then you can add them right before the $.ajax call.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问