ajax发布-我想更改Accept-Encoding标头值 [英] ajax post - I want to change the Accept-Encoding header value

查看:690
本文介绍了ajax发布-我想更改Accept-Encoding标头值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery ajax通过HTTP POST调用WCF服务.响应是GZIP编码的,这在我的环境中引起了问题. (请参阅此问题).如果响应不是GZIP编码的,则一切正常.

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.

因此,在Fiddler中,我看到jQuery生成的查询具有以下标头:

So looking in Fiddler, I see that the jQuery generated query has the following headers:

Accept-Encoding: gzip,deflate,sdch

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

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.

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

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.

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

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);

推荐答案

此值稍后可能会被覆盖.

This value is probably being overwritten later in the process.

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

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.

尝试实现下面的演示代码中所示的beforeSend,并且标头值现在应该到达最终请求(手指交叉).

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);

这篇关于ajax发布-我想更改Accept-Encoding标头值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆