表单提交Ajax使用jQuery有时不工作



我正在做表单数据提交使用Ajax与jQuery。当我在弹出窗口上提交表单时,刷新父页面。我的代码:

$(document).ready(function() {
    $("#frm_addSpeedData").submit(function(event) {  
    //event.preventDefault();
    $.ajax({
         type: "POST", 
          url: "/webapp/addSpeedDataAction.do", 
          data: $(this).serialize(),
          success: function(data) {
            //console.log("Data: " + data);
            window.opener.location.reload(); 
          }
        });
    });
});

然而,页面在回调成功时刷新,但我无法在父页面上看到更新。有时我能看到更新,有时不能。问题是什么?我还需要知道我如何写它在本地javascript和使用ajax javascript提交表单。

也许你得到这个错误,由于javascript是异步的,你的代码将继续,即使你还没有从请求的响应。

试试这个:

$(document).ready(function() {
        $("#frm_addSpeedData").submit(function(event) {  
        //event.preventDefault();
        $.ajax({
             type: "POST", 
              url: "/webfdms/addSpeedDataAction.do", 
              data: $(this).serialize(),
              async: false, // This will only proceed after getting the response from the ajax request.
              success: function(data) {
                //console.log("Data: " + data);
                window.opener.location.reload(); 
              }
            });
        });
    });

最新更新