回调不会在JQuery Mobile多页面中触发

时间:2013-06-03 17:59:50

标签: ios jquery jquery-mobile cordova

我正在把头发拉出来......我有一个单独的.html页面代表我的移动应用程序的主页。在同一个html页面中,我将页面定义为登录屏幕。显示登录屏幕的逻辑工作得很好......我的问题是我有一个具有以下签名的登录功能......

ajaxLogin(credentials, successCallback, failureCallback) {
    $.ajax({
            url: SERVER_API + '/login',
            data: credentials,
            type: 'post',
            async: true,
            beforeSend: function () {
                $.mobile.showPageLoadingMsg(true);
            },
            always: function () {
                console.log('always');
                $.mobile.hidePageLoadingMsg();
            },
            done: function (data, status, xhr) {
                console.log('login: ok'); //< -- This never fires but I can see that the server returned a 200 in Safari's developer menu
                successCallback();
            },
            fail: function (request, error) {
                failureCallback(); // <-- This function is never executed when I send bad login info. But again, the server is returning an error status code
            }
        });
}

我认为这会非常简单......我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

beforeSend 函数是jQuery Ajax设置jQuery.ajax([settings])的一部分。但是始终完成失败是jqXHR对象的promise方法。在您的示例中,您使用始终完成失败作为不正确的设置。

$.ajax({
    url: "http://.....",
    beforeSend: function (xhr) {
        // code
    }
}).done(function (data) {
    // do something
});

您可以在jQ Ajax documentation

中找到更多信息

答案 1 :(得分:0)

我认为你正在寻找“成功”和“错误”,即:

ajaxLogin(credentials, successCallback, failureCallback) {
    $.ajax({
        url: SERVER_API + '/login',
        data: credentials,
        type: 'post',
        async: true,
        success: successCallback,
        error:  failureCallback
    });
}

然后你可以在原来的这个函数调用中处理你的成功/失败:

ajaxLogin(
    credentialsData, //data you're passing to the function
    function (data)
    {
        //stuff to do on success
    },
    function (jqXHR, statusText, errorThrown)
    {
        //stuff to do on failure
    }
);