【问题标题】:Return the 500 error message using jQuery .load() or .get()使用 jQuery .load() 或 .get() 返回 500 错误消息
【发布时间】:2011-08-27 07:39:55
【问题描述】:

我正在尝试使用 jQuery .load() 方法进行 AJAX 调用。当请求通过时,它会正确加载返回数据。如果我收到 500 错误,则不会。有没有办法也输出失败的请求消息信息?

$("#activity").load("/forumsetup", { id:myid }, 
       function(data) {
          $("#restart").css("visibility","visible");
        });  

我可以在 firebug 中看到它,但想将它加载到我的页面上。

【问题讨论】:

标签: jquery exception-handling httpwebrequest


【解决方案1】:

来自 jQuery 文档页面:http://api.jquery.com/load/

$("#success").load("/not-here.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});

【讨论】:

  • 很好,做到了。就我而言,一个经典的 RTFM 案例。
  • 呵呵。好吧,我记得阅读 jQuery 添加支持以检测所有请求功能上的失败请求,所以这只是一个快速查找。不过,如果您对答案感到满意,请将其标记为答案。
【解决方案2】:

是的,jquery 在 xhr 中返回一个对象。您只需通过以下方式访问它:

$("#success").load("/not-here.php", function(response, status, xhr) {
if ( status == "error" ) 
{
 if (xhr.status == 500 )
  do something...

更多信息,请查看:http://api.jquery.com/jQuery.ajax/#jqXHR

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多