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

时间:2022-01-14 20:16:57

I am apologize for the stupid question, but I need your help. I need to get information about response inside AJAX.

我为这个愚蠢的问题道歉,但我需要你的帮助。我需要获取有关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
          }
    });

How can I use response body? (documentation Jquary.Ajax is not working at the momment)

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

1 个解决方案

#1


16  

The first param to error handler is jqxhr, it has the property responseText which will give the response body.

错误处理程序的第一个参数是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
          }
    });

#1


16  

The first param to error handler is jqxhr, it has the property responseText which will give the response body.

错误处理程序的第一个参数是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
          }
    });