gpt4 book ai didi

jquery - 如何显示 $.ajax 调用返回的错误?

转载 作者:行者123 更新时间:2023-12-03 22:07:29 25 4
gpt4 key购买 nike

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

编辑:我找不到请求的网址,但我确信该网址:是我的主机上的有效网址,可能出了什么问题?我什至可以直接在浏览器中访问它。

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

最佳答案

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

error : function(jqXHR, textStatus, errorThrown){

}

这是jQuery documentation对于此事件:

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.

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

关于jquery - 如何显示 $.ajax 调用返回的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11432834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com