ajax post - 我想更改Accept-Encoding标头值

And*_*erd 9 ajax wcf jquery header

我正在使用jQuery ajax通过HTTP POST调用我的WCF服务.响应是GZIP编码的,这会导致我的环境出现问题.(见这个问题).如果响应不是GZIP编码,一切都很好.

所以看着Fiddler,我看到jQuery生成的查询有以下标题:

Accept-Encoding: gzip,deflate,sdch
Run Code Online (Sandbox Code Playgroud)

如果,通过fiddler,我将此值更改为None,则响应不会被压缩,这就是我想要的.我需要做的就是更改"Accept-Encoding"标头中的值.

似乎无法通过.ajax命令更改此标头值.(见本论坛帖子).

任何人都可以告诉我有什么选项可以更改此标头值.

这是我目前的尝试.我的headers参数似乎被忽略了.

    $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);
Run Code Online (Sandbox Code Playgroud)

tim*_*ode 5

该值可能在此过程的后面被覆盖。

引用:http ://api.jquery.com/jQuery.ajax/
标头(默认值:{})说明
类型:PlainObject
与请求一起发送的附加标头键/值对的对象。该设置是在调用beforeSend函数之前设置的;因此,标头设置中的任何值都可以从beforeSend函数中覆盖

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);
Run Code Online (Sandbox Code Playgroud)


uza*_*y95 5

这是不可能的,因为浏览器选择了正确的编码类型。如果你这样做

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');
},......
Run Code Online (Sandbox Code Playgroud)

你会看到这个错误:

Refused to set unsafe header "Accept-Encoding"
Run Code Online (Sandbox Code Playgroud)

参考:App Engine 接受编码


tim*_*ode 0

我不确定“无”是一个有效的选项。我相信,如果您将标头设置为接受编码“deflate”而不是“none”,那么应该可以解决您的问题。

例如

headers: { 'Accept-Encoding' : 'deflate' }
Run Code Online (Sandbox Code Playgroud)