Ajax post - I want to change the value of the Accept-Encoding header

I am using jQuery ajax to call my WCF service using HTTP POST. The response is encrypted with gzip 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 generated jQuery query has the following headers:

Accept-Encoding: gzip,deflate,sdch 

If through fiddler I changed this value to None , then the answer is not compressed, and this is what I want. All I have to do is change the value in the "Accept-Encoding" header.

It seems like this header value cannot be changed with the .ajax . (See this forum post ).

Can someone tell me what parameters I need to change this header value.

Here is 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); 
+6
source share
3 answers

This value is likely to be overwritten later in the process.

Link: http://api.jquery.com/jQuery.ajax/
headers (default: {}) description
Type: PlainObject
An object of additional key / value pairs to send along with the request. This parameter is set before calling the beforeSend function; therefore, any values ​​in the header settings can be overwritten from the beforeSend function .

Try to implement beforeSend , as shown in the demo code below, and the value (s) of the header should be received until the last request (crossed fingers).

 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
source

This is not possible due to the choice of the correct type of encoding by the browser. If you do it

 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 will see this error:

 Refused to set unsafe header "Accept-Encoding" 

Link: Accept-Encoding for App Engine

+2
source

I'm not sure that 'none' is a valid option. I believe that if you set the headers to accept the deflate encoding rather than none, this should solve your problem.

eg.

 headers: { 'Accept-Encoding' : 'deflate' } 
0
source

Source: https://habr.com/ru/post/900654/


All Articles