javascript - 为什么 'error' 属性不是 ajax 的有效属性或方法?

标签 javascript ajax visual-studio-cordova

我有一个移动 Cordova 应用程序。当我运行它并尝试登录时。我收到以下错误:

JavaScript 运行时错误:对象不支持属性或方法“错误”

这是错误引用的代码块:

 $.ajax({
           url: url,
           type: 'POST',
           data: { domainName: domain, username: username, password: password, appId: appId }


       })

           .done(function (json) {


               if (json.success) {
                   that.set("isLoggedIn", true);
                   token = json.token;
                   username = username;
                   isAuthenticated = true;
                   $('#show-Loader').hide();
                  window.location("#esig");


               }
               else {
                   navigator.notification.alert("No User Found", function () { }, "Login failed", 'OK');
                   //alert(json.error);
                   $('#show-Loader').hide();
                   return;
               }

           })
           //For some reason the following 'Property or Method' is not supported by $.ajax() 
      .error(function (xhr, status, error) {
          navigator.notification.alert('Unable to Connect to Server' + '\n' + ' Please check Settings.', function () { }, "Connection Failed", 'OK');
          $('#show-Loader').hide();
      });
   },

有人知道为什么我会收到此 JavaScript 错误吗?本质上我希望它能够解决无法连接到服务器的错误。这就是我正在测试的。

最佳答案

使用.fail()而不是.error()。 (.error() 不存在)

你可以这样做

$.ajax({
    success: function() { ... },
    error: function() { ... }
});

$.ajax({ ... })
    .done(function() { ... })
    .fail(function() { ... })
    .always(function() { ... });  //<-- optional

推荐后者。

关于javascript - 为什么 'error' 属性不是 ajax 的有效属性或方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126338/

相关文章:

javascript - 如何在 JavaScript 中 append &lt;script&gt;&lt;/script&gt;?

javascript - 在单元格上的两篇论文之间拖放 - JointJs

javascript - 将延迟处理程序附加到函数外部的嵌套 AJAX 调用

javascript - 当ajax代码工作时,使用javascript禁用html中的链接

javascript - JQuery .replaceWith 函数仅提供原始 html

android - Visual Studio 2017 Cordova ;更改 Android 构建的目标文件夹

javascript - 将 var 定义为 0.5 与 0.5 相比有什么区别?

javascript - Protractor 异步执行问题

visual-studio-cordova - 更新 Node 和 NPM VS Cordova 更新 5

visual-studio - 尝试将 Cordova 应用程序与商店关联时 Visual Studio 崩溃