【问题标题】:responseType in $.ajax$.ajax 中的响应类型
【发布时间】:2013-01-03 05:17:21
【问题描述】:
$.ajax({
   type: "POST",
   url: "bla",
   xhrFields: { responseType: "document" },
   data: {},
   success: function(arg,arg2,request){
      console.log(request.responseXML)
   }
})

为什么打印“未定义”?我该如何解决这个问题?

【问题讨论】:

  • 除了下面的答案我必须指出,URL bla 必须告诉浏览器内容类型是application/json。

标签: jquery xml ajax jqxhr


【解决方案1】:

您期待 JSON 返回吗?尝试时会发生什么:

$.ajax({
  type: "POST",
  url: "bla",
  dataType: 'xml',
}).done(function (response) {
   console.log(response);
});

如果您查看 jQuery's documentation,他们会概述如何:

$.ajax() 返回的 jQuery XMLHttpRequest (jqXHR) 对象 jQuery 1.5 是浏览器原生 XMLHttpRequest 的超集 目的。例如,它包含 responseText 和 responseXML 属性,以及 getResponseHeader() 方法。

因此,响应变量包含您需要的内容。要查看其结构,请发送console.log() 并转到开发者工具 (Chrome) 或 Firebug (Firefox) 中的“控制台”选项卡。

【讨论】:

  • 我想获取 url 的 XML 而不是 JSON。
  • 谢谢,这是我需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 2012-02-14
  • 2011-04-14
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
相关资源
最近更新 更多