jQuery:获取Ajax响应的类型

时间:2022-06-04 20:16:05

I need to make an Ajax request, but its response may vary and it's decided on the server side.

我需要发出一个Ajax请求,但它的响应可能会有所不同,这取决于服务器端。

Is there any way to know what type the response is?

有没有办法知道响应是什么类型?

It may look similar to:

它可能看起来类似于:

$.post(url, pars, function (response, type) {
    if (type=='json') ...
    if (type=='html') ...
});

2 个解决方案

#1


2  

There's no built-in way to do this, it's determined and tossed away by jQuery.httpData (note: it will be jquery.ajax.httpData in 1.4.3).

这没有内置的方法,它是由jQuery决定并丢弃的。httpData(注意:它将是jquery.ajax。httpData 3)。

Though you can take a look at the httpData source and run the same functions yourself, that's a bit wasteful, since jQuery's doing it already. I

尽管您可以查看httpData源并自己运行相同的函数,但这有点浪费,因为jQuery已经这样做了。我

If your choices are only json or html, you could check typeof response, it should be "string" for HTML, otherwise you have JSON, which you could also check and be sure about as well, for example: type && type.propertyAlwaysThere.

如果您的选择只是json或html,您可以检查响应类型,它应该是html的“string”,否则您有json,您也可以检查并确保它,例如:type & type. propertyalwaysthere。

#2


2  

If you have control of the server-side code as well, the easiest thing will probably be to include a parameter with a value to specify the format.

如果您也可以控制服务器端代码,那么最简单的方法可能是包含一个参数,其中包含一个值来指定格式。

Here's an example where I did the same type of thing you're describing. I loaded a table with customer values from data returned in xml, json, or string format, all driven by the value my server-side code returned as the format parameter:

这里有一个例子,我做了和你描述的一样的事情。我从xml、json或字符串格式返回的数据中加载了一个包含客户值的表,所有这些都是由我服务器端代码作为格式参数返回的值驱动的:

function checkCusts(id, format, resultRegion) {
  var address = "cust-lookup.jsp";
  var data = "cust_id_list=" + getValue(id) + "&format=" + format;

  if (address != "") {
    ajaxPost(address, data,
    function(request) {
      parseCustomers(request, format, resultRegion);
    });
  }
}

function parseCustomers(request, format, resultRegion) {
  if ((request.readyState == 4) && (request.status == 200)) {
    var headings = new Array("Customer ID", "First Name", "Last Name", "Balance");
    var rows = null, customers = null;

    if ("xml" == format) {
      var xmlDocument = request.responseXML;
      customers = xmlDocument.getElementsByTagName("customer");
      rows = new Array(customers.length);
      var subElementNames = ["cust_id", "first_name", "last_name", "balance"];
      for (var i=0; i<customers.length; i++) {
        rows[i] = getElementValues(customers[i], subElementNames);
      }
    } else if ("json" == format) {
      var rawData = request.responseText;
      var data = eval("(" + rawData + ")");
      rows = data.customers;
    } else if ("string" == format) {
      var rawData = request.responseText;
      var rowStrings = rawData.split(/[\n\r]+/);
      rows = new Array(rowStrings.length -1);
      for (var i=1; i<rowStrings.length; i++) {
        rows[i-1] = rowStrings[i].split("#");
      }
    }

    var table = getTable(headings, rows);
    htmlInsert(resultRegion, table);
  }
}

#1


2  

There's no built-in way to do this, it's determined and tossed away by jQuery.httpData (note: it will be jquery.ajax.httpData in 1.4.3).

这没有内置的方法,它是由jQuery决定并丢弃的。httpData(注意:它将是jquery.ajax。httpData 3)。

Though you can take a look at the httpData source and run the same functions yourself, that's a bit wasteful, since jQuery's doing it already. I

尽管您可以查看httpData源并自己运行相同的函数,但这有点浪费,因为jQuery已经这样做了。我

If your choices are only json or html, you could check typeof response, it should be "string" for HTML, otherwise you have JSON, which you could also check and be sure about as well, for example: type && type.propertyAlwaysThere.

如果您的选择只是json或html,您可以检查响应类型,它应该是html的“string”,否则您有json,您也可以检查并确保它,例如:type & type. propertyalwaysthere。

#2


2  

If you have control of the server-side code as well, the easiest thing will probably be to include a parameter with a value to specify the format.

如果您也可以控制服务器端代码,那么最简单的方法可能是包含一个参数,其中包含一个值来指定格式。

Here's an example where I did the same type of thing you're describing. I loaded a table with customer values from data returned in xml, json, or string format, all driven by the value my server-side code returned as the format parameter:

这里有一个例子,我做了和你描述的一样的事情。我从xml、json或字符串格式返回的数据中加载了一个包含客户值的表,所有这些都是由我服务器端代码作为格式参数返回的值驱动的:

function checkCusts(id, format, resultRegion) {
  var address = "cust-lookup.jsp";
  var data = "cust_id_list=" + getValue(id) + "&format=" + format;

  if (address != "") {
    ajaxPost(address, data,
    function(request) {
      parseCustomers(request, format, resultRegion);
    });
  }
}

function parseCustomers(request, format, resultRegion) {
  if ((request.readyState == 4) && (request.status == 200)) {
    var headings = new Array("Customer ID", "First Name", "Last Name", "Balance");
    var rows = null, customers = null;

    if ("xml" == format) {
      var xmlDocument = request.responseXML;
      customers = xmlDocument.getElementsByTagName("customer");
      rows = new Array(customers.length);
      var subElementNames = ["cust_id", "first_name", "last_name", "balance"];
      for (var i=0; i<customers.length; i++) {
        rows[i] = getElementValues(customers[i], subElementNames);
      }
    } else if ("json" == format) {
      var rawData = request.responseText;
      var data = eval("(" + rawData + ")");
      rows = data.customers;
    } else if ("string" == format) {
      var rawData = request.responseText;
      var rowStrings = rawData.split(/[\n\r]+/);
      rows = new Array(rowStrings.length -1);
      for (var i=1; i<rowStrings.length; i++) {
        rows[i-1] = rowStrings[i].split("#");
      }
    }

    var table = getTable(headings, rows);
    htmlInsert(resultRegion, table);
  }
}