【问题标题】:Callbacks don't fire in JQuery Mobile multi-page回调不会在 JQuery Mobile 多页面中触发
【发布时间】:2013-06-03 17:59:49
【问题描述】:

我在这个问题上大吃一惊。我有一个 .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
            }
        });
}

我以为这很简单……我错过了什么吗?

【问题讨论】:

    标签: ios jquery jquery-mobile cordova


    【解决方案1】:

    在 beforeSend EM>功能是jQuery的Ajax的设置jQuery.ajax([设置])的一部分。然而总是 EM>,完成 EM>和失败 EM>是jqXHR对象的承诺方法。在您的例子中,你已经使用的总是 em>的完成 EM>和失败 em>的作为设置,这是不正确的。 P>

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

    您可以找到@ 987654321更多信息@。 P>

    【讨论】:

      【解决方案2】:

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

      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
          }
      );
      

      【讨论】:

        猜你喜欢
        • 2012-09-11
        • 1970-01-01
        • 2012-02-21
        • 1970-01-01
        • 2016-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多