jQuery ajax 接受属性有什么意义?它真的有什么作用吗? [英] What is the point of jQuery ajax accepts attrib? Does it actually do anything?

查看:18
本文介绍了jQuery ajax 接受属性有什么意义?它真的有什么作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花了整整一个小时试图弄清楚为什么会这样(咖啡脚本)

Spent a solid hour trying to sort out why on earth this (coffeescript)

$.ajax
  accepts: "application/json; charset=utf-8"

完全没有改变接受头,而这个

$.ajax
  dataType: "json"

正确地将接受头设置为 application/json;charset=utf-8

properly sets the accepts header to application/json; charset=utf-8

完全糊涂,是我遗漏了什么还是接受属性是全年愚人节的玩笑?

Totally confused, am I missing something or is the accepts attrib a year-round April Fool's joke?

推荐答案

一如既往的文档 是你的朋友:

As always the documentation is your friend:

接受

默认值:取决于数据类型

Default: depends on DataType

发送的内容类型请求标头告诉服务器它将做出什么样的响应接受作为回报.如果接受设置需要修改,则为建议在 $.ajaxSetup() 方法中这样做一次.

The content type sent in the request header that tells the server what kind of response it will accept in return. If the accepts setting needs modification, it is recommended to do so once in the $.ajaxSetup() method.

数据类型

默认:智能猜测(xml、json、脚本或 html)

Default: Intelligent Guess (xml, json, script, or html)

您期望从服务器返回的数据类型.如果没有指定,jQuery 将尝试根据 MIME 类型推断它响应(XML MIME 类型将产生 XML,在 1.4 JSON 将产生一个 JavaScript 对象,在 1.4 中的脚本将执行该脚本,并且其他任何内容都将作为字符串返回).可用的类型(和作为第一个参数传递给成功回调的结果)是:

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

xml":返回一个可以通过jQuery处理的XML文档.

"xml": Returns a XML document that can be processed via jQuery.

"html":以纯文本形式返回 HTML;评估包含的脚本标签插入 DOM 时.

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

"script":将响应评估为JavaScript 并将其作为纯文本返回.通过附加禁用缓存一个查询字符串参数,_=[TIMESTAMP],除非缓存选项设置为真.注意:这会将 POST 转换为 GET远程域请求.

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, _=[TIMESTAMP], to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.

"json":将响应评估为 JSON 和返回一个 JavaScript 对象.在 jQuery 1.4 中,JSON 数据被解析为严谨的态度;任何格式错误的 JSON 都会被拒绝,并且会出现解析错误抛出.(有关正确的 JSON 格式的更多信息,请参阅 json.org.)

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

"jsonp":使用 JSONP 加载到 JSON 块中.添加一个额外的?callback=? 添加到 URL 的末尾以指定回调.禁用通过附加查询字符串参数进行缓存,
_=[TIMESTAMP],到URL,除非缓存选项设置为 true.

"jsonp": Loads in a JSON block using JSONP. Adds an extra ?callback=? to the end of your URL to specify the callback. Disables caching by appending a query string parameter,
_=[TIMESTAMP], to the URL unless the cache option is set to true.

"text":纯文本细绳.多个空格分隔的值:从 jQuery 1.5 开始,jQuery 可以将 dataType 从它在 Content-Type 标头中收到的内容转换为你需要什么.例如,如果您希望文本响应是视为 XML,请使用text xml"作为数据类型.你也可以做一个JSONP 请求,将其作为文本接收,并由 jQuery 解释为XML:jsonp 文本 xml."类似地,诸如jsonp"之类的速记字符串xml" 将首先尝试从 jsonp 转换为 xml,并且失败即,从 jsonp 转换为 text,然后从 text 转换为 xml.

"text": A plain text string. multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

现在回到你的问题.我不熟悉 cofeescript 但与 dataType 是一个字符串相反,accepts 参数是一个映射,应该像这样使用:

Now back to your problem. I am not familiar with cofeescript but contrary to dataType which is a string, the accepts parameter is a map and should be used like this:

$.ajax({
    url: ...
    dataType: 'json',
    accepts: {
        xml: 'text/xml',
        text: 'text/plain'
    }
});

这篇关于jQuery ajax 接受属性有什么意义?它真的有什么作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆