jQuery ajax接受attrib的意义是什么?它实际上做了什么吗?

编程入门 行业动态 更新时间:2024-10-27 02:17:29
本文介绍了jQuery ajax接受attrib的意义是什么?它实际上做了什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

花了一个小时试图理清为什么这个(coffeescript)

$。ajax 接受:application / json; charset = utf-8

确实没有 改变接受头,而这

$。ajax dataType:json

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

完全困惑,我是否错过了某些东西,或者是一年四季愚人节玩笑的接受属性?

解决方案

与往常一样文档是您的朋友:

接受

默认值:取决于数据类型

在请求标头中发送的内容类型告诉服务器它将采用何种响应接受回报。如果接受设置需要修改,建议在$ .ajaxSetup()方法中执行一次。

dataType

默认值:智能猜测(xml,json,script或html)

期待从服务器回来。如果没有指定,jQuery将尝试根据的MIME类型推断它的响应(XML MIME类型将产生XML,在1.4中JSON将产生1.4 b $ ba JavaScript对象,在1.4脚本将执行该脚本,其他任何内容将作为字符串返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)为: $ b xml :一个可以通过jQuery处理的XML文档。 $ b html :以纯文本形式返回HTML;包含的脚本标记在插入到DOM中时被评估为。

脚本:将响应评估为 JavaScript并将其作为纯文本返回。除非高速缓存选项设置为true,否则通过在URL中追加查询字符串参数 _ = [TIMESTAMP] 来禁用高速缓存。注意:这会将POST转换为GET 远程域请求。 $ b json :将响应评估为JSON,返回JavaScript对象。在jQuery 1.4中,JSON数据以严格的方式在中解析;任何格式不正确的JSON都会被拒绝,并且抛出一个解析错误。 (请参阅json以获取有关正确JSON格式的更多信息。)

jsonp :使用JSONP加载JSON块。在您的网址末尾添加额外的 ?callback =?以指定回叫。通过在 URL中追加一个查询字符串参数 - _ = [TIMESTAMP] 来禁用缓存,除非缓存选项设置为true。

text :纯文本字符串。多个空格分隔的值:从jQuery 1.5开始,jQuery可以将ContentType类型头文件中的数据类型转换为所需的数据类型。例如,如果您希望将文本响应作为对待为XML,请使用 text xml 作为dataType。您还可以创建的JSONP请求,将其作为文本接收,并由jQuery解释为 XML:jsonp text xml。同样,诸如jsonp xml之类的简写字符串将首先尝试从jsonp转换为xml,并且失败,从jsonp转换为文本,然后从文本转换为xml。

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

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

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

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

did absolutely nothing to change the accepts header, while this

$.ajax dataType: "json"

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:

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

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接受attrib的意义是什么?它实际上做了什么吗?

本文发布于:2023-11-02 14:03:54,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上做   意义   jQuery   ajax   attrib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!