AJAX - 得到响应主体在成功和错误(AJAX - get response body in s

2019-08-17 19:07发布

我的愚蠢的问题道歉,但我需要你的帮助。 我需要得到有关内部响应信息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
          }
    });

如何使用响应主体? ( 文档Jquary.Ajax不是在momment工作)

Answer 1:

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


文章来源: AJAX - get response body in success and error