jQuery ajax post - 我想更改 Accept-Encoding 标头值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7988947/
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
ajax post - I want to change the Accept-Encoding header value
提问by Andrew Shepherd
I am using jQuery ajax to call my WCF service with an HTTP POST. The response is GZIP encoded, and this causes problems in my environment. (See this question). If the response is not GZIP encoded everything is fine.
我正在使用 jQuery ajax 通过 HTTP POST 调用我的 WCF 服务。响应是 GZIP 编码的,这会导致我的环境出现问题。(见这个问题)。如果响应不是 GZIP 编码的,一切都很好。
So looking in Fiddler, I see that the jQuery generated query has the following headers:
因此,在 Fiddler 中,我看到 jQuery 生成的查询具有以下标题:
Accept-Encoding: gzip,deflate,sdch
If, via fiddler, i change this value to None
, then the response is not compressed, which is what I want. All that I need to do is change the value in the "Accept-Encoding" header.
如果我通过提琴手将此值更改为None
,则响应不会被压缩,这正是我想要的。我需要做的就是更改“Accept-Encoding”标头中的值。
It seems that it is not possible to change this header value via the .ajax
command. (See this forum post).
似乎无法通过.ajax
命令更改此标头值。(请参阅此论坛帖子)。
Can anyone tell me what options I have to change this header value.
谁能告诉我有哪些选项可以更改此标头值。
Here's my current attempt. My headers
parameter seems to be ignored.
这是我目前的尝试。我的headers
参数似乎被忽略了。
$telerik.$.ajaxSetup({
accepts: 'application/json, text/javascript, */*'
});
var parameters = {
"playerId": args.playerId
};
var dataInJsonFormat = '{ "playerId": ' + args.playerId + '}';
var ajaxCallParameters = {
accepts: 'application/json, text/javascript, */*',
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
url: "../Services/CmsWebService.svc/SendUpdateRequestToPlayer",
headers: { "Accept-Encoding" : "None" },
type: "POST",
data: dataInJsonFormat,
dataType: 'json',
error: function (jqXHR, textStatus, errorThrown) {
var errorString = 'Error thrown from ajax call: ' + textStatus + 'Error: ' + errorThrown;
var displayPanel = document.getElementById('requestStatusUpdateResults');
$telerik.$(displayPanel).text(errorString);
},
success: function (data, textStatus, jqXHR) {
var displayPanel = document.getElementById('requestStatusUpdateResults');
$telerik.$(displayPanel).text(data.d);
}
};
$telerik.$.ajax(ajaxCallParameters);
回答by timecode
This value is probably being overwritten later in the process.
此值可能会在此过程的后期被覆盖。
Ref: http://api.jquery.com/jQuery.ajax/
headers (default: {}) description
Type: PlainObject
An object of additional header key/value pairs to send along with the request. This setting is set before the beforeSend function is called; therefore, any values in the headers setting can be overwritten from within the beforeSend function.
参考:http://api.jquery.com/jQuery.ajax/
标头(默认:{}) 描述
类型:PlainObject
与请求一起发送的附加标头键/值对的对象。此设置在调用 beforeSend 函数之前设置;因此,headers 设置中的任何值都可以从 beforeSend 函数中被覆盖。
Try implementing beforeSend
as seen in the demo code below and the header value(s) should get to the final request now (fingers crossed).
尝试beforeSend
按照下面的演示代码中所示的方式实现,标头值现在应该到达最终请求(手指交叉)。
var ajaxParams = {
accepts: 'text/html',
async: true,
cache: false,
contentType: 'text/html',
url: 'http://www.google.com',
type: 'GET',
beforeSend: function (jqXHR) {
// set request headers here rather than in the ajax 'headers' object
jqXHR.setRequestHeader('Accept-Encoding', 'deflate');
},
success: function (data, textStatus, jqXHR) {
console.log('Yay!');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Oh no!');
},
complete: function (jqXHR, textStatus) {
console.log(textStatus);
console.log(jqXHR.status);
console.log(jqXHR.responseText);
}
};
$.ajax(ajaxParams);
回答by uzay95
This is not possible because of choosing the right encoding type by browser. If you do this
这是不可能的,因为浏览器选择了正确的编码类型。如果你这样做
var ajaxParams = {
accepts: 'text/html',
async: true,
cache: false,
contentType: 'text/html',
url: 'http://www.google.com',
type: 'GET',
beforeSend: function (jqXHR) {
// set request headers here rather than in the ajax 'headers' object
jqXHR.setRequestHeader('Accept-Encoding', 'deflate');
},......
You'll see this error:
你会看到这个错误:
Refused to set unsafe header "Accept-Encoding"
回答by timecode
I'm not sure that 'none' is a valid option there. I believe if you set the headers to accept encoding 'deflate' rather than 'none', that should sort out your problem.
我不确定那里的“无”是一个有效的选项。我相信如果您将标题设置为接受编码“放气”而不是“无”,那应该可以解决您的问题。
e.g.
例如
headers: { 'Accept-Encoding' : 'deflate' }