jquery - jQuery ajax 接受 attrib 有什么意义?它实际上有什么作用吗?

标签 jquery types header set response

花了整整一个小时试图弄清楚这到底是为什么(coffeescript)

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

绝对没有做任何事情来更改接受 header ,而这

$.ajax
  dataType: "json"

正确地将accept头设置为application/json;字符集=utf-8

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

最佳答案

一如既往的documentation是你的 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.

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

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

关于jquery - jQuery ajax 接受 attrib 有什么意义?它实际上有什么作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065252/

相关文章:

php - 在 PHP 中指定类的对象类型的方法

types - 如何在 postscript 文件中嵌入真正的字体

sql - MySQL表中varchar长度的重要性

php - PHP 中的 while 和 file_exists

html - 标题内带有 url 图片的表格

c++ - 有人可以帮助阐明头文件的工作原理吗?

javascript - 请帮我修复这个 Jquery 函数,并找出一个回退以防 Javascript 被禁用

jquery - 左键单击 + 按住 = 添加复选框

javascript - 在 asp.net mvc 应用程序中使用 href 属性进行重定向不起作用

javascript - 分隔符淡入淡出