AJAX - 获得成功和错误的响应体

时间:2013-03-20 07:03:03

标签: jquery ajax response

我为这个愚蠢的问题道歉,但我需要你的帮助。我需要获取有关AJAX内部响应的信息。

$.ajax({
          type: "POST",
          url: '/register',
          data : registerRequestJSON,
          contentType:"application/json",
          success: function(data){
              $("#register_area").text();// need to show success
          },
          error: function(err) {
            $("#register_area").text("@text"); // @text = response error, it is will be errors: 324, 500, 404 or anythings else
          }
    });

我如何使用响应体? (documentation Jquary.Ajax没有在妈妈那里工作)

1 个答案:

答案 0 :(得分:18)

错误处理程序的第一个参数是jqxhr,它具有属性responseText,它将为响应主体提供。

$.ajax({
          type: "POST",
          url: '/register',
          data : registerRequestJSON,
          contentType:"application/json",
          success: function(data){
              $("#register_area").text();// need to show success
          },
          error: function(jqxhr) {
            $("#register_area").text(jqxhr.responseText); // @text = response error, it is will be errors: 324, 500, 404 or anythings else
          }
    });
相关问题