ajax post - I want to change the Accept-Encoding header value
Solution 1
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.
Try implementing beforeSend
as seen in the demo code below and the header value(s) should get to the final request now (fingers crossed).
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);
Solution 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"
Ref: App Engine Accept-Encoding
![Andrew Shepherd](https://i.stack.imgur.com/WurYy.jpg?s=256&g=1)
Andrew Shepherd
Started my first job as a full-time developer in 1996. Spent 11 years coding solely in C++. Then moved on to C#, then got a job where everyone is coding in Matlab, then took over a digital signage solution, and have been doing that since 2011. Now, on any given day I code in C#, Powershell, Javascript, Typescript, Java and Transact SQL, writing applications for Chrome, Android or Windows devices and the web which isn't really a device. In my wayward youth before taking on programming I went and got a music degree. I am experimenting to see if this link gets removed from my profile: https://au.gofundme.com/f/stop-stack-overflow-from-defaming-its-users
Updated on July 19, 2022Comments
-
Andrew Shepherd almost 2 years
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.
So looking in Fiddler, I see that the jQuery generated query has the following headers:
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.It seems that it is not possible to change this header value via the
.ajax
command. (See this forum post).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.$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);
-
Andrew Shepherd over 11 yearsThanks for your answer, but that isn't actually the problem. The problem is that I can't change the 'Accept-Encoding' value to anything, neither 'none' nor 'deflate'.
-
timecode over 11 yearsAh, I see. My longer answer should help then.