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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3741574/
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 16:00:58  来源:igfitidea点击:

jquery how to check response type for ajax call

jqueryajax

提问by sam

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

回答by Ankit Jaiswal

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 但它似乎更可靠。

回答by Imdad

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

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

i.e.

IE

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.

如果使用此方法,则不必在成功回调中写入 2 个额外参数。

回答by tyrion

If the response is parsed as JSON, the jqXHRobject will have a responseJSONproperty.

如果响应被解析为 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 属性可用。

回答by SolidALb

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
    }}

回答by Shamim Hafiz

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.
}