javascript 使用 gzip 编码的 ajax 发送请求不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10345996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ajax send request with encoding gzip is not working
提问by MANISHDAN LANGA
Ajax send request with encoding gzip (iis7) is not working below are the code for send request can some one help me what is wrong in my code.
带有编码 gzip (iis7) 的 Ajax 发送请求在下面不起作用是发送请求的代码有人可以帮助我我的代码有什么问题。
Thanks in advance
提前致谢
function sendRequest(url, callback, postData)
{
var req = createXMLHTTPObject();
if (!req) {
return;
}
var method = (postData) ? "POST" : "GET";
req.open(method, "xml/" + url, true);
req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
if (postData) {
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.setRequestHeader("Content-Encoding", "gzip");
}
req.onreadystatechange = function() {
}
req.send(postData);
}
采纳答案by Denys Séguret
The problem doesn't seem to be related to header but to compression.
问题似乎与标头无关,而与压缩有关。
You don't seem to compress your postData.
您似乎没有压缩您的 postData。
If postData is already compressed, no need to try to manually set content-encoding.
如果 postData 已经被压缩,则无需尝试手动设置 content-encoding。
If it is not, either let the browser negotiate the transfer encoding with the server (this is part of the protocol and done automatically, the server saying if it accepts it, but I think that's rarely the case) or (if you really really need to) encode it yourself. This SO question indicates a library to compress browserside : JavaScript implementation of Gzip
如果不是,要么让浏览器与服务器协商传输编码(这是协议的一部分并自动完成,服务器说它是否接受它,但我认为这种情况很少发生)或(如果你真的需要到)自己编码。这个 SO 问题表明一个库来压缩浏览器端:Gzip 的 JavaScript 实现
回答by tuoxie007
Considering the security, browser does not allow you to override some headers including "Content-Encoding".
考虑到安全性,浏览器不允许您覆盖某些标头,包括“内容编码”。
回答by robocat
One way to transparently have the requests for your XMLHttpRequest highly compressed is to use HTTP/2 (e.g. serve your website via CloudFlare).
一种透明地高度压缩 XMLHttpRequest 请求的方法是使用 HTTP/2(例如,通过 CloudFlare 为您的网站提供服务)。
When using HTTP/2, then although the HTTP headers do not say Content-Encoding: gzip
the underlying HTTP/2 protocol compresses everything.
当使用 HTTP/2 时,虽然 HTTP 标头没有说Content-Encoding: gzip
底层的 HTTP/2 协议压缩了一切。
It also compresses much better than gzip because:
它还比 gzip 压缩得更好,因为:
- it compresses headers
- header compression uses a standard dictionary
- I think data compression builds a dictionary over multiple messages (brotli - I haven't double-checked that though)
- 它压缩标题
- 标头压缩使用标准字典
- 我认为数据压缩在多条消息上构建了一个字典(brotli - 不过我没有仔细检查过)
You can see if your serveris using HTTP/2 by:
您可以通过以下方式查看您的服务器是否正在使用 HTTP/2:
- Open Chrome, and F12 to open developer tools
- Click on the network tab
- close the request inspector panel (has tabs
Headers Preview Response Timing
) - Right click on the Name header of the list of requests and tick
Protocol
- Navigate to your website and watch what protocol is used for all requests - in the protocol column you want to see
h2
nothttp/1.1
- 打开Chrome,F12打开开发者工具
- 单击网络选项卡
- 关闭请求检查器面板(有选项卡
Headers Preview Response Timing
) - 右键单击请求列表的名称标题并勾选
Protocol
- 导航到你的网站和手表使用什么协议的所有要求-在协议中列你想看到
h2
不http/1.1
I wouldn't recommend using JavaScript compression libraries because that causes slowdown and inefficiencies.
我不建议使用 JavaScript 压缩库,因为这会导致速度减慢和效率低下。