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.
如果,通過fiddler,我將此值更改為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.
這是我目前的嘗試。我的header參數似乎被忽略了。
$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);
3 个解决方案
#1
3
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/ headers(默認值:{})description類型:PlainObject要與請求一起發送的其他標頭鍵/值對的對象。在調用beforeSend函數之前設置此設置;因此,可以從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);
#2
2
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"
參考:App Engine接受編碼
#3
0
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.
我不確定'none'是否是一個有效的選擇。我相信如果你設置標題接受編碼'deflate'而不是'none',那應該解決你的問題。
e.g.
例如
headers: { 'Accept-Encoding' : 'deflate' }