Is it possible to force jQuery to make AJAX calls for URLs with gzip/deflate enabled?

送分小仙女□ 提交于 2019-11-26 16:27:16

问题


I have a web service that is willing to output gzip/deflated data. I've verified that the service will respond with raw JSON or with gzip'd JSON using wget and curl.

I want to consume this web service using the jQuery AJAX call.

By default, the $.ajax call that jQuery provides does not add the "Accept-Encoding: gzip" HTTP request header that's necessary for the web server to respond with gzipped data.

However, when I use jQuery's own methods to add the header, eg:

$.ajax({url: 'http://foo.com/service.json',
        beforeSend: function(xhr) { 
            console.log('xhr set'); 
            xhr.setRequestHeader('Accept-Encoding', 'deflate') 
       } 
});

then the following error appears in the browser console:

Refused to set unsafe header "Accept-Encoding"

Is it possible to force jQuery to make AJAX calls for URLs with gzip/deflate enabled?

If not, is this a shortcoming in jQuery, or something more fundamental with AJAX?


回答1:


Browsers automatically add the accept-encoding header as appropriate, including on XHR requests. You don't need to do that at the DOM/JS level.



来源:https://stackoverflow.com/questions/3778706/is-it-possible-to-force-jquery-to-make-ajax-calls-for-urls-with-gzip-deflate-ena

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!