weixin_33738555 2018-05-21 16:57 采纳率: 0%
浏览 53

Ajax异步发布

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条回答 默认 最新

  • George_Fal 2018-05-21 17:08
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办