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

Ulu*_*yev 15 ajax jquery 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
          }
    });
Run Code Online (Sandbox Code Playgroud)

我怎样才能使用响应体?(文档Jquary.Ajax不在妈妈的工作)

Aru*_*hny 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
          }
    });
Run Code Online (Sandbox Code Playgroud)