Jquery 在更改页面之前调用ajax?

Jquery 在更改页面之前调用ajax?,jquery,ajax,Jquery,Ajax,示例代码: $.ajax({ url: someUrl, data: someData, dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", success: function ()

示例代码:

        $.ajax({
                url: someUrl,
                data: someData,
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function () { },
        });                

        window.location = urlToGoTo;
假设上面的代码在用户单击按钮时运行

由于ajax调用是异步的,当上述代码运行时会发生什么?使用像Fiddler这样的工具,看起来有时ajax调用成功,有时它永远不会被调用,即在ajax调用有机会运行之前更改当前网页

在这种情况下,您是否应该在设置window.location(例如在ajaxcomplete事件中)之前始终等待ajax调用完成


只是想了解引擎盖下发生的事情

您可以在成功回调中执行重定向

$.ajax({
    url: someUrl,
    data: someData,
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function () {
        window.location = urlToGoTo;
    }
});

必须在ajax调用中包括重定向链接代码,尤其是在success函数中:

 $.ajax({
                url: someUrl,
                data: someData,
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function () { 
                window.location = urlToGoTo;
                },
                error:function(){
                alert("Error Occurred!"); 
                }
        });