Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax post - I want to change the Accept-Encoding header value

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);
like image 469
Andrew Shepherd Avatar asked Nov 03 '11 00:11

Andrew Shepherd


1 Answers

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);
like image 59
timecode Avatar answered Sep 19 '22 08:09

timecode