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

后端 未结 3 1967
醉梦人生
醉梦人生 2021-02-04 11:38

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 i

相关标签:
3条回答
  • 2021-02-04 11:41

    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' }
    
    0 讨论(0)
  • 2021-02-04 11:48

    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

    0 讨论(0)
  • 2021-02-04 11:55

    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);
    
    0 讨论(0)
提交回复
热议问题