0

我正在使用 jQuery 的 UI Dialogmy-ui-dialog在屏幕上绘制一个对话框 ( ) 并向用户展示一些<input>s 和其他控件。当用户单击对话框的“确定”按钮时,我希望 UI 对话框向我的服务器(通过getJSON)触发 AJAX/JSON 消息,允许服务器处理该调用,然后在收到来自服务器的响应后关闭对话框。然后用户应该被重定向(通过window.location)到另一个网页。

这是我的代码:

$("#my-ui-dialog").dialog({
    modal: true,
    autoOpen: false,
    height: 255,
    width: 300,
    buttons: {
        "OK" : function() {
            var f = $("#fizz").val();

            $.getJSON(
                "/myserver/do-something",
                {
                    fizz: f
                },
                function() {
                    $("#my-ui-dialog").dialog('destroy');
                }
            );

            // When I leave this in the code works great.
            // When I comment the alert out, the getJSON call never
            // hits the server-side.
            alert("Returned from the backend...");

            window.location = "/myserver/some-other-url"
            $(this).dialog("close");
        },
        Cancel : function() {
            $(this).dialog("close");
        }
    }
});

当我把那个alert盒子留在里面并等待几秒钟(警报框绘制到屏幕上)时,该getJSON方法似乎有足够的时间来访问服务器并返回。但是,如果我注释掉alert该请求,则该请求甚至都不会到达我的服务器。我可以通过在监听的处理程序的开头添加一条日志消息来说明这一点/myserver/do-something。这也是一个 Java Web 应用程序,我没有看到任何 Tomcat 收到请求的证据,也没有看到HTTP使用 Firebug 调试时生成的请求。所有这 3 个都表明请求根本没有命中服务器。

我在这里有什么选择?我可以让 jQuery 休眠几秒钟吗?这感觉像是一个丑陋的解决方案。必须有更好的方法来使用 jQuery API...在此先感谢!

4

1 回答 1

4

关闭对话框并window.locationgetJSON回调中编写代码。这就是为什么会有回调。

于 2012-12-14T21:29:22.647 回答