是否有可能迫使jQuery的做出了用gzip网址AJAX调用/放气启用? [英] Is it possible to force jQuery to make AJAX calls for URLs with gzip/deflate enabled?

查看:420
本文介绍了是否有可能迫使jQuery的做出了用gzip网址AJAX调用/放气启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,愿意输出GZIP /瘪的数据。我已经验证该服务将与原始JSON或使用wget和卷曲gzip压缩的JSON响应。

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.

我想使用jQuery AJAX调用消耗该Web服务。

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

默认情况下,$就调用jQuery提供不添加接受编码:gzipHTTP请求头这是必要的Web服务器用gzip压缩的数据作出响应

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.

然而,当我使用jQuery的自己的方法来添加标题,例如:

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"

是否有可能迫使jQuery的做出了用gzip网址AJAX调用/放气启用?

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

如果没有,这是一个缺点在jQuery中,或一些与AJAX更根本的?

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

推荐答案

浏览器会自动添加Accept-Encoding头酌情包括XHR请求。你并不需要做的是在DOM / JS的水平。

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.

这篇关于是否有可能迫使jQuery的做出了用gzip网址AJAX调用/放气启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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