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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。