Javascript 将头参数传递给 jquery ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12347211/
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
Pass accepts header parameter to jquery ajax
提问by aWebDeveloper
When I inspect the following code in Chrome Console it shows me a Request header Accept:undefined
当我在 Chrome 控制台中检查以下代码时,它会向我显示一个请求标头 Accept:undefined
jQuery.ajax({
url: _this.attr('href'),
accepts: "application/json; charset=utf-8",
});
});
How do I set accept type as json. I don't want to set a custom header or use beforeSend
如何将接受类型设置为 json。我不想设置自定义标题或使用beforeSend
回答by karthick
Try this ,
尝试这个 ,
$.ajax({
headers: {
Accept: "text/plain; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
}
data: "data",
success : function(response) {
// ...
}
});
See this post for reference:
请参阅此帖子以供参考:
回答by gaurang171
There two alternate ways to set accept header, which are as below:
有两种设置接受头的替代方法,如下所示:
1) setRequestHeader('Accept','application/json; charset=utf-8');
2) $.ajax({
dataType: ($.browser.msie) ? "text" : "json",
accepts: {
text: "application/json"
}
});
回答by koppor
In recent versions of jQuery, setting "dataType" to an appropriate value also sets the accepts header. For instance, dataType: "json"
sets the accept header to Accept: application/json, text/javascript, */*; q=0.01
.
在最新版本的 jQuery 中,将“dataType”设置为适当的值也会设置接受标头。例如,dataType: "json"
将接受标头设置为Accept: application/json, text/javascript, */*; q=0.01
.
回答by Roman Royter
The other answers do not answer the actual question, but rather provide workarounds which is a shame because it literally takes 10 seconds to figure out what the correct syntax for accepts
parameter.
其他答案没有回答实际问题,而是提供了解决方法,这是一种耻辱,因为实际上需要 10 秒钟才能找出accepts
参数的正确语法。
The accepts
parameter takes an object which maps the dataType
to the Accept
header. In your case you don't need to even need to pass the accepts
object, as setting the data type to json
should be sufficient. However if you do want to configure a custom Accept
header this is what you do:
该accepts
参数采用一个将 映射dataType
到Accept
标头的对象。在您的情况下,您甚至不需要传递accepts
对象,因为将数据类型设置为json
应该就足够了。但是,如果您确实想配置自定义Accept
标头,这就是您要做的:
accepts: {"*": "my custom mime type" },
accepts: {"*": "my custom mime type" },
How do I know? Open jquery's source code and search for "accepts". The very first find tells you all you need to know:
我怎么知道?打开 jquery 的源代码并搜索“accepts”。第一个发现告诉你所有你需要知道的:
accepts: {
"*": allTypes,
text: "text/plain",
html: "text/html",
xml: "application/xml, text/xml",
json: "application/json, text/javascript"
},
As you see the are default mappings to text
, html
, xml
and json
data types.
正如你看到的是默认的映射text
,html
,xml
和json
数据类型。
回答by Srinivas
Try this:
尝试这个:
$.ajax({
beforeSend: function (xhr){
xhr.setRequestHeader("Content-Type","application/json");
xhr.setRequestHeader("Accept","text/json");
},
type: "POST",
//........
});
回答by Nathan Phillips
You had already identified the accepts parameter as the one you wanted and keyur is right in showing you the correct way to set it, but if you set DataType to "json" then it will automatically set the default value of accepts to the value you want as per the jQuery reference. So all you need is:
您已经将 accepts 参数确定为您想要的参数,并且 keyur 正确地向您展示了设置它的正确方法,但是如果您将 DataType 设置为“json”,那么它会自动将 accepts 的默认值设置为您想要的值根据jQuery 参考。所以你只需要:
jQuery.ajax({
url: _this.attr('href'),
dataType: "json"
});
回答by Googol
Although some of them are correct, I've found quite confusing the previous responses. At the same time, the OP asked for a solution without setting a custom header or using beforeSend
, so I've being looking for a clearer explanation. I hope my conclusions provide some light to others.
尽管其中一些是正确的,但我发现之前的回答非常令人困惑。同时,OP 在不设置自定义标头或使用的情况下要求解决方案beforeSend
,因此我一直在寻找更清晰的解释。我希望我的结论能给其他人一些启发。
The code
编码
jQuery.ajax({
....
accepts: "application/json; charset=utf-8",
....
});
doesn't work because accepts
must be a PlainObject
(not a String
) according to the jQuery doc (http://api.jquery.com/jquery.ajax/). Specifically, jQuery expect zero or more key-value pairs relating each dataType
with the accepted MIME type for them. So what I've finally using is:
不起作用,因为根据 jQuery 文档(http://api.jquery.com/jquery.ajax/)accepts
必须是 a PlainObject
(不是 a )。具体来说,jQuery 期望零个或多个键值对与它们接受的 MIME 类型相关联。所以我最终使用的是:String
dataType
jQuery.ajax({
....
dataType: 'json',
accepts: {
json: 'application/json'
},
....
});
回答by iainH
I use jQuery.getJSON( url [, data ] [, success( data, textStatus, jqXHR ) ] )
for example:
我使用jQuery.getJSON( url [, data ] [, success( data, textStatus, jqXHR ) ] )
例如:
var url="my.php";
$.getJSON( url, myObj )
.done(function( json ) { ... }) /* got JSON from server */
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Failed to obtain JSON data from server: " + err );
}); /* failed to get JSON */
getJSON is shorthand for:
getJSON 是以下的简写:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});