jQuery AJAX - 在成功和错误中获取响应体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15517283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:14:16  来源:igfitidea点击:

AJAX - get response body in success and error

jqueryajaxresponse

提问by Ulug'bek Ro'zimboyev

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.Ajaxis not working at the momment)

如何使用响应体?(文档 Jquary.Ajax目前不起作用)

回答by Arun P Johny

The first param to error handler is jqxhr, it has the property responseTextwhich 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
          }
    });