dataType vs接受-Ajax请求 [英] dataType vs accepts - Ajax Request
问题描述
我试图了解Ajax请求中accepts和dataType之间的区别.文档状态:
接受(默认值:取决于DataType) 类型:PlainObject 请求标头中发送的内容类型告诉服务器它将作为回报接受哪种响应.
dataType(默认值:Intelligent Guess(xml,json,脚本或html)) 类型:字符串 您期望从服务器返回的数据类型.
基本上,是相同的吗?具有相同的目的.
下面是一个准确的答案:
通过accepts
选项,您可以更改请求中的Accept
标头
更改此选项时,请求中的Accept
标头将设置为指定的标头.请注意,它不是字符串,而是映射接受的响应的MIME类型的对象.类似于{ text: "text/plain", html: "text/html" }
.服务器可以使用Accept
标头以请求所期望的格式提供响应,如果服务器不能以请求所期望的一种格式提供响应,则失败.
一个非常重要的事情是,至少在jQuery 1.11.3(我测试过的地方)中,此选项似乎不起作用,相反,我设法使用headers
选项更改了标头:headers: {Accept : "text/json"}
. /p>
dataType
选项可让您预处理响应
如果定义dataType
,则请求的响应将由jQuery进行预处理,然后才可用于成功处理程序.例如:
如果指定了
json
,则在将响应作为对象传递给成功处理程序之前,将使用jQuery.parseJSON
解析响应.如果指定了
script
,则$.ajax()
将执行从服务器接收到的JavaScript,然后将其作为字符串传递给成功处理程序.
更多示例此处,在数据类型"部分.
如果未设置dataType
,则响应的Content-Type
将确定应对响应进行哪些预处理.请注意,更改dataType
也会更改Accept
标头.通常,无需自己更改Accept
标头.
示例
request.php
<?php
if(strpos($_SERVER["HTTP_ACCEPT"],"text/javascript") === false)
exit("I only provide text/javascript responses");
echo "alert('This is my response!')";
index.html
<button id="send">Send</button>
<div id="response"></div>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(function(){
$("#send").click(function(){
$.ajax({
method: "GET",
url: "request.php",
dataType: "script" // Change this to "text" and you will see the difference
}).done(function(data) {
$("#response").text(data);
});
});
});
</script>
当dataType
设置为"script"
时,Accept
标头将包含"text/javascript"
,因此对request.php
的测试将通过.它将返回"alert('This is my response!')"
,并且由于dataType
设置为"script"
,jQuery会尝试将其作为javascript执行,然后将其作为纯文本传递给成功处理程序.
如果将dataType
更改为"text"
,则Accept
标头将不包含"text/javascript"
,因此在request.php
上的测试将失败.它将返回"I only provide text/javascript responses"
,并且由于dataType
设置为"text"
,jQuery会将其作为纯文本传递给成功处理程序.
I am trying to understand the difference between accepts and dataType in a Ajax Request. Documentation states:
accepts (default: depends on DataType) Type: PlainObject The content type sent in the request header that tells the server what kind of response it will accept in return.
dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server.
Basically, Is it the same?, it has the same purpose.
Here is a, hopefully, precise answer:
The accepts
option let you change the Accept
header in the request
When you change this option, the Accept
header in the request will be set to the one(s) specified. Take notice that it is not a string, but an object mapping the MIME type of the accepted responses. Like { text: "text/plain", html: "text/html" }
. The Accept
header can be used by the server to provide the response in the format expected by the request, or fail in case it can't provide the response in one of the formats expected by the request.
A really important thing is that, at least in jQuery 1.11.3 (where I tested), this options seems to not be working, instead I managed to change the header using the headers
option: headers: {Accept : "text/json"}
.
The dataType
option let you pre-process the response
If you define a dataType
, the response of the request will be pre-processed by jQuery before being available to the succes handler. For example:
If
json
is specified, the response is parsed usingjQuery.parseJSON
before being passed, as an object, to the success handler.If
script
is specified,$.ajax()
will execute the JavaScript that is received from the server before passing it on to the success handler as a string.
More examples here, in the "Data Types" section.
In case that dataType
is not set, the Content-Type
of the response will determine what pre-processing should be done to the response. Be aware that changing the dataType
will change the Accept
header too. Usually there is no need to change the Accept
header by yourself.
Example
request.php
<?php
if(strpos($_SERVER["HTTP_ACCEPT"],"text/javascript") === false)
exit("I only provide text/javascript responses");
echo "alert('This is my response!')";
index.html
<button id="send">Send</button>
<div id="response"></div>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(function(){
$("#send").click(function(){
$.ajax({
method: "GET",
url: "request.php",
dataType: "script" // Change this to "text" and you will see the difference
}).done(function(data) {
$("#response").text(data);
});
});
});
</script>
When the dataType
is set to "script"
the Accept
header will include "text/javascript"
so the test on request.php
will pass. It will return "alert('This is my response!')"
and because the dataType
is set to "script"
jQuery will attempt to execute that as javascript and then pass it as plain text to the success handler.
If you change the dataType
to "text"
the Accept
header will NOT include "text/javascript"
so the test on request.php
will fail. It will return "I only provide text/javascript responses"
and because the dataType
is set to "text"
jQuery will pass it as plain text to the success handler.
这篇关于dataType vs接受-Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!