jquery如何检查ajax调用的响应类型

时间:2022-03-11 07:16:23

How can I determine the response type of ajax call in Jquery? At times, the server sends json response and at times it sends only the html for display purposes. Right now I am using

如何确定Jquery中ajax调用的响应类型?有时,服务器发送json响应,有时只发送html用于显示目的。现在我正在使用

if(response.indexOf('Error'))
  //popup error message
else
 response.username
 response.address

5 个解决方案

#1


107  

You can try it like:

你可以这样尝试:

$.ajax({
  type: "POST",
  url: "your url goes here", 
  data: "data to be sent", 
  success: function(response, status, xhr){ 
    var ct = xhr.getResponseHeader("content-type") || "";
    if (ct.indexOf('html') > -1) {
      //do something
    }
    if (ct.indexOf('json') > -1) {
      // handle json here
    } 
  }
});

Basically it is also using indexOf but it seems more reliable.

基本上,它也在使用indexOf,但似乎更可靠。

#2


14  

You can simply use javascript's easy method to check the type

您可以简单地使用javascript的easy方法来检查类型

i.e.

即。

if(typeof response=="object")
{
 // Response is javascript object
}
else
{
 // Response is HTML
}

If you use this method you don't have to write 2 extra parameter in the success callback.

如果您使用此方法,您不必在success回调中编写两个额外的参数。

#3


8  

The answers above didnt work for me so I came up with this solution:

上面的答案对我来说并不管用,所以我想出了这个办法:

success: function(data, textStatus , xhr) {
if(xhr.responseXML.contentType == "text/html") {
    //do something with html
    }
else if(xhr.responseXML.contentType == "application/json") {
    //do something with json
    }}

#4


6  

If the response is parsed as JSON, the jqXHR object will have a responseJSON property.

如果响应被解析为JSON, jqXHR对象将拥有一个responseJSON属性。

$.ajax(
    // ...
).done(function(data, textStatus, jqXHR) {
    if (jqXHR.responseJSON) {
        // handle JSON
    } else {
        // handle html
    }
}).fail(function(jqXHR, textStatus, errorThrown) {
    if (jqXHR.responseJSON) {
        // handle JSON
    else {
        // handle html
    }
})

From the jQuery.ajax documentation:

jQuery。ajax文档:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

如果指定了json,则使用jQuery解析响应。将parseJSON作为对象传递给成功处理程序之前。解析后的JSON对象通过jqXHR对象的responseJSON属性可用。

#5


0  

To accept a JSON reply, you can set the reply type as JSON. I usually design my server side code so they always return JSON reply. In the event it fails to do so for whatever reason, I would get an error in my AJAX call for having incorrect JSON format and I can process the reply from server as not being non JSON.

要接受JSON应答,可以将应答类型设置为JSON。我通常设计我的服务器端代码,所以它们总是返回JSON回复。在这种情况下,无论出于什么原因,它都不能这样做,我在AJAX调用中会遇到一个错误,因为JSON格式不正确,我可以将服务器的回复处理为非JSON。

error: function(response, status, xhr){ 
// do something with the reply.
}

#1


107  

You can try it like:

你可以这样尝试:

$.ajax({
  type: "POST",
  url: "your url goes here", 
  data: "data to be sent", 
  success: function(response, status, xhr){ 
    var ct = xhr.getResponseHeader("content-type") || "";
    if (ct.indexOf('html') > -1) {
      //do something
    }
    if (ct.indexOf('json') > -1) {
      // handle json here
    } 
  }
});

Basically it is also using indexOf but it seems more reliable.

基本上,它也在使用indexOf,但似乎更可靠。

#2


14  

You can simply use javascript's easy method to check the type

您可以简单地使用javascript的easy方法来检查类型

i.e.

即。

if(typeof response=="object")
{
 // Response is javascript object
}
else
{
 // Response is HTML
}

If you use this method you don't have to write 2 extra parameter in the success callback.

如果您使用此方法,您不必在success回调中编写两个额外的参数。

#3


8  

The answers above didnt work for me so I came up with this solution:

上面的答案对我来说并不管用,所以我想出了这个办法:

success: function(data, textStatus , xhr) {
if(xhr.responseXML.contentType == "text/html") {
    //do something with html
    }
else if(xhr.responseXML.contentType == "application/json") {
    //do something with json
    }}

#4


6  

If the response is parsed as JSON, the jqXHR object will have a responseJSON property.

如果响应被解析为JSON, jqXHR对象将拥有一个responseJSON属性。

$.ajax(
    // ...
).done(function(data, textStatus, jqXHR) {
    if (jqXHR.responseJSON) {
        // handle JSON
    } else {
        // handle html
    }
}).fail(function(jqXHR, textStatus, errorThrown) {
    if (jqXHR.responseJSON) {
        // handle JSON
    else {
        // handle html
    }
})

From the jQuery.ajax documentation:

jQuery。ajax文档:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

如果指定了json,则使用jQuery解析响应。将parseJSON作为对象传递给成功处理程序之前。解析后的JSON对象通过jqXHR对象的responseJSON属性可用。

#5


0  

To accept a JSON reply, you can set the reply type as JSON. I usually design my server side code so they always return JSON reply. In the event it fails to do so for whatever reason, I would get an error in my AJAX call for having incorrect JSON format and I can process the reply from server as not being non JSON.

要接受JSON应答,可以将应答类型设置为JSON。我通常设计我的服务器端代码,所以它们总是返回JSON回复。在这种情况下,无论出于什么原因,它都不能这样做,我在AJAX调用中会遇到一个错误,因为JSON格式不正确,我可以将服务器的回复处理为非JSON。

error: function(response, status, xhr){ 
// do something with the reply.
}