我正在使用$ .post向JSP发送查询,这会为我返回一些非常棒的数据。
如果查询格式错误,页面将返回“页面错误”和HTTP状态 500内部服务器错误
在jQuery中,如何检测此错误,以便告诉用户失败?
runQuery : function () {
$.post(
admin_stats.runQueryURL,
{
buster : Math.random,
statsQuery: admin_stats.getQuery(),
jsp: 'admin_statsQuery'
},
admin_stats.handleStatsQuery,
"html"
);
返回的数据是一个HTML表,此表现在已足够。
另外:如果这是丑陋或不是我应该做的事情,那么完全接受批评=)
答案 0 :(得分:3)
您需要使用$.ajax
方法:
$.ajax({
type: 'POST',
url: admin_stats.runQueryURL,
data: {
buster : Math.random,
statsQuery: admin_stats.getQuery(),
jsp: 'admin_statsQuery'
},
success: admin_stats.handleStatsQuery,
error: admin_stats.error, //change this to your handler
dataType: "html"
});
有关详细信息,请参阅此处:http://api.jquery.com/jQuery.ajax/
答案 1 :(得分:1)
看看.ajaxError
。来自文档:
注册要在何时调用的处理程序 Ajax请求完成时出错。 这是一个Ajax事件。
记录示例:
$('.log').ajaxError(function(e, xhr, settings, exception) {
if (settings.url == 'ajax/missing.html') {
$(this).text('Triggered ajaxError handler.');
}
});
如果要提供特定于返回的错误类型的消息,可以这样执行:
$('.log').ajaxError(function(e, xhr, settings, exception) {
if(xhr.status == 500) {
$(this).text('internal server error');
} else if (xhr.status == 404) {
$(this).text('page not found');
}
// etc.
});