15

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

编辑:我收到请求的 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;
});
4

5 回答 5

18

errorjQuery函数的事件$.ajax接收3个参数

error : function(jqXHR, textStatus, errorThrown){

}

这是此事件的jQuery 文档

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

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

于 2012-07-11T12:46:22.367 回答
9

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

error: function(xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                },
于 2015-05-14T12:11:51.233 回答
2

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

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

根据此问题的($.ajax 实用程序中的 JQuery 错误选项)答案,数据(错误)对象的可能值

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
于 2012-07-11T12:42:37.903 回答
0

改变这个

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

这样

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

:)

于 2017-10-13T21:14:31.933 回答
0

您无法使用警报语句检查对象数据

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

error: function(data){
     console.log(data);
}
于 2019-09-18T15:15:48.237 回答