weixin_33733810 2013-01-31 18:35 采纳率: 0%
浏览 66

jQuery ajax页面重新加载

We are making multiple ajax requests to "save" data in a web app, then reload the page. We have run into a situation where (since requests are made asynchronously) the page is reloaded while or before the ajax calls are completed. The simple solution to this was to make the ajax calls with the "async": false option on, forcing synchronous calls. This seems to work, however dialog box code that runs BEFORE any calls are executed delay in running.

Any advice is greatly appreciated!

Also it should be noted that putting an alert() before the reload ALLOWS the ajax requests to be made. (The alert is obviously delaying the reload long enough for the requests to successfully go through)

UPDATED with code samples:

$(".submit_button").click(function(){ 
    popupMessage();
    sendData(); //the ajax calls are all in here
    location.reload();
});


function sendData() {
    //a bunch of these:
    $.ajax({
    "dataType": "text",
    "type": "POST",
    "data": data,
    "url": url,
    "success": function (msg) {}
    }).done(function( msg ) {

    }); 
}
  • 写回答

3条回答 默认 最新

  • weixin_33739541 2013-01-31 18:52
    关注

    it depends on way you do your requests For example (you don't do form submit. Otherwise you need prevent form submission)

    $.ajax({
       url: 'some_url',
       type:    'GET',
       data: 'var1=value1&var2=value2',
     success: function(){
       //do smth
     },
     error: function(){
       alert(w.data_error);
       document.location.reload(); 
     }
     complete: function(){ //A function to be called when the request finishes (after success and error callbacks are executed) - from jquery docs
       //do smth if you need
       document.location.reload(); 
     }
    });
    

    Take a look onto complete block

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序