jQuery ajax 接受 attrib 有什么意义?它真的有什么作用吗?

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

What is the point of jQuery ajax accepts attrib? Does it actually do anything?

jquerytypesheadersetresponse

提问by virtualeyes

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

花了一个小时试图弄清楚这到底是为什么(咖啡脚本)

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

did absolutely nothingto change the accepts header, while this

没有绝对没有改变接受头,而这

$.ajax
  dataType: "json"

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

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

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

完全困惑,我错过了什么还是接受属性是全年愚人节的玩笑?

采纳答案by Darin Dimitrov

As always the documentationis your friend:

一如既往,文档是您的朋友:

accepts

Default: depends on DataType

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.

dataType

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

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": Returns a XML document that can be processed via jQuery.

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

"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": 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": 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": 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.

接受

默认值:取决于数据类型

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

数据类型

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

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

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

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

script”:将响应作为 JavaScript 进行评估,并将其作为纯文本返回。_=[TIMESTAMP]除非缓存选项设置为 true,否则通过将查询字符串参数 , 附加到 URL 来禁用缓存。注意:这会将 POST 转换为远程域请求的 GET。

json”:将响应评估为 JSON 并返回一个 JavaScript 对象。在 jQuery 1.4 中,JSON 数据以严格的方式解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。(有关正确 JSON 格式的更多信息,请参阅 json.org。)

jsonp”:使用 JSONP 加载到 JSON 块中。?callback=?在您的 URL 末尾添加额外内容 以指定回调。
_=[TIMESTAMP]除非缓存选项设置为 true,否则通过将查询字符串参数 , 附加到 URL 来禁用缓存。

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

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

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

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