Angular gunzip json file automatically : Refused t

2019-02-17 09:48发布

问题:

I use a gzip json file static file on my server (actually, it's my test server) and the data i receive is always compressed.

Here is my code :

    $http({ 
      url :'app/assets/json/makes2v.json.gz',
      method: "GET",
      headers: { 'Accept-Encoding': 'gzip' }})
    .success(function(data, status, headers, config) {
      console.log(data);
  });

I use angular 1.3.15 and Chrome .

In the console i have this error:

Refused to set unsafe header "Accept-Encoding"

Any help will be appreciated.

Thanks,

回答1:

You shouldn't set the request header "Accept-Encoding". This is set automatically by the browser. See this Q&A for a list a browsers that accept gzip.

On the server side, you will need to set the Content-Encoding to:

Content-Encoding: gzip

Then the response should be automatically decompressed by the browser.