2011-11-03 6 views
6

jQuery ajaxを使用して、HTTP POSTでWCFサービスを呼び出しています。応答はGZIPでエンコードされており、これが私の環境に問題を引き起こします。 (this questionを参照)。応答がGZIPでエンコードされていない場合は、すべて正常です。ajax post - Accept-Encodingヘッダーの値を変更したい

だから、フィドラーで見て、私はjQueryのクエリは次のヘッダーを持って生成されたことを参照してください。

Accept-Encoding: gzip,deflate,sdch 

シオマネキを経て、私はNoneにこの値を変更し、場合、応答は何である、圧縮されていませんが欲しいです。私がする必要があるのは、 "Accept-Encoding"ヘッダーの値を変更することだけです。

.ajaxコマンドでこのヘッダー値を変更することはできないようです。 (this forum post参照)。

このヘッダー値を変更する必要があるオプションは誰にでも教えてください。

これは私の現在の試みです。私の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); 
+0

んこの回答のヘルプ:http://stackoverflow.com/questions/5771878/jqueryを-ajax-request-change-user-agent – sberry

答えて

0

私は「none」が有効なオプションであるとは確信していません。私はあなたの問題を整理する必要がありますが、 'none'ではなく 'deflate'をエンコードするようにヘッダーを設定すると信じています。

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

あなたの答えをありがとうが、実際には問題ではありません。問題は、「Accept-Encoding」の値を「none」でも「deflate」でも変更できないことです。 –

+0

ああ、そうです。私のより長い答えが助けになるはずです。 – timecode

2

この値は、処理の後半で上書きされる可能性があります。

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.

以下デモコードとヘッダ値に見られるように(指が交差)は、最終的な要求に取得する必要があり(S)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); 
2

ブラウザで適切なエンコードタイプを選択したため、これはできません。 この

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'); 
},...... 

を行う場合は、このエラーが表示されます:

Refused to set unsafe header "Accept-Encoding" 

参考:App Engine Accept-Encoding