3

在我的 Windows 小工具代码中,在 SettingsClosing(event) 中使用 jQuery 进行了 ajax (POST) 调用。我通过Fiddler观看 ajax 调用执行。Ajax 调用正在返回所需的结果。但它没有触发成功回调 ajax。我在主视图的 OnPageLoad() 事件中尝试了相同的代码,它在那里工作正常,我的代码是这样的

function Login(username, password) {
    var url = "http://example.com/Auth.php";
    var data = { pusername: username, ppassword: password };
    jQuery.support.cors = true;
    $.ajax({
        type: "POST"
            , dataType: "json"
            , url: url, data: data
            , success: function (refresh_token) {
                System.Gadget.Settings.write("success", "true");
                System.Gadget.Settings.write("dd", refresh_token);
                var tok = refresh_token['refreshtoken'];
                var name = refresh_token['name'];
                System.Gadget.Settings.write("name", name);
                System.Gadget.Settings.write("refreshtoken", tok);

            }
                , error: function (XMLHttpRequest, textStatus, errorThrown) {
                    System.Gadget.Settings.write("login_error", errorThrown);
                    System.Gadget.Settings.write("login_STATUS", textStatus);
                }
    });
}

function SettingsClosing(event) 
{
    //use this question to now if setting was closed with OK button
    if (event.closeAction == event.Action.commit)
    {
        var username = $('#txtpUsername').val();
        var password = $('#txtpPassword').val();
        Login(username, password);

    }
}

任何帮助表示赞赏。

4

1 回答 1

3

因为 ajax 调用是异步的,所以在操作完成之前代码似乎没有机会运行。您可以尝试设置event.cancel为 true,然后在 ajax 调用完成后手动触发关闭。MSDN 参考: http: //msdn.microsoft.com/en-us/library/windows/desktop/ms723677 (v=vs.85).aspx

于 2012-09-18T14:41:23.570 回答