【问题标题】:How to display error returned by $.ajax call?如何显示 $.ajax 调用返回的错误?
【发布时间】:2012-07-11 01:57:41
【问题描述】:

我有一个需要永远加载的代码,最后当我放置错误处理程序时它会显示警报,但我需要知道它返回了什么错误?我怎么知道?

编辑:我收到请求的 url 未找到,但我确定 url: 是我主机上的有效 URL,可能有什么问题?我什至可以直接在浏览器中访问它。

// process logging in a user from sidebar
$("#login-form").submit(function(event) {
    $('input:submit').attr("disabled", true);
    $("p.form-result").empty();
    $('p.form-submit').after('<p class="loading"><img src="<?php bloginfo('template_directory'); ?>/img/loading.gif" alt="" /></p>');
    $.ajax({
        url: '<?php bloginfo('template_directory'); ?>/ajax/login.php',
        type: 'POST',
        data: $(this).serialize(),
        dataType: 'json',
        success: function(data){
            $('.loading').remove();
            $('input:submit').attr("disabled", false);
            if (data.status) {
                // success
                $("p.form-result").html('<span class="success">' + data.message + '</span>');
                window.setTimeout(function(){location.reload()},3000);
            } else {
                // error
                $("p.form-result").html('<span class="error">' + data.message + '</span>');
            }
        },
        error: function(data){
            alert('error');
        }
    });
    return false;
});

【问题讨论】:

  • 您是否尝试过alert(data)console.log(data) 而不是alert('error')
  • 您是否尝试过检查您的 error 函数中的 data 变量?
  • alert(data.message);(如果 data.message 包含有用的内容)
  • 我在这里尝试了一个答案stackoverflow.com/questions/9676084/…,它返回了未找到请求的网址。

标签: jquery


【解决方案1】:

jQuery函数$.ajaxerror事件接收3个参数

error : function(jqXHR, textStatus, errorThrown){

}

这是此活动的jQuery documentation

请求失败时调用的函数。函数接收 三个参数:jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象,一个 描述发生的错误类型和可选的字符串 异常对象,如果发生。第二个可能的值 参数(除了 null)是“超时”、“错误”、“中止”和 “解析器错误”。当发生 HTTP 错误时,errorThrown 会收到 HTTP 状态的文本部分,例如“未找到”或“内部” 服务器错误。”从 jQuery 1.5 开始,错误设置可以接受一个数组 的功能。每个函数都会被依次调用。注意:这个处理程序 跨域脚本和 JSONP 请求不调用。这是一 Ajax 事件。

您将能够知道参数textStatus的错误是什么

【讨论】:

    【解决方案2】:

    当 ajax 调用中包含一些无效参数时,将执行 ajax 调用中的错误事件。以下函数将通过引发错误并在警报中显示错误定义来帮助您理解错误代码。

    error: function(xhr, ajaxOptions, thrownError){
                        alert(xhr.status);
                    },
    

    【讨论】:

      【解决方案3】:

      使用错误函数的数据参数来警告错误及其属性。它模仿了您的实际错误。

      error: function(data){
           alert(data);
      }
      

      根据此问题 (JQuery error option in $.ajax utility) 的答案,数据(错误)对象的可能值

      timeout - when your specified timeout is exceeded
      error - http error, like 404
      notmodified - when requested resource was not modified since last request
      parsererror - when an xml/json response is bad
      

      【讨论】:

        【解决方案4】:

        改变这个

        url: '<?php bloginfo('template_directory'); ?>/ajax/login.php',
        

        由此

        url: '<?php bloginfo("template_directory"); ?>/ajax/login.php',
        

        :)

        【讨论】:

          【解决方案5】:

          您不能使用警报语句检查对象数据

          使用console.log(data)检查错误

          error: function(data){
               console.log(data);
          }
          

          【讨论】:

            猜你喜欢
            • 2012-08-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-13
            • 2011-06-10
            相关资源
            最近更新 更多