javascript 从 XMLHttpRequest 中删除 HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5159166/
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
Removing HTTP headers from an XMLHttpRequest
提问by Mikola
I am working on an ajax long polling type application, and I would like to minimize the amount of bandwidth I am using. One of the big costs right now are the client side HTTP headers. Once I have a connection established and a session id stored on the client, I don't really want to squander any more bandwidth transferring redundant http information (such as browser type, accept encodings, etc.). Over the course of many connections, this quickly adds up to a lot of data!
我正在开发一个 ajax 长轮询类型的应用程序,我想尽量减少我使用的带宽量。目前最大的成本之一是客户端 HTTP 标头。一旦我建立了连接并将会话 ID 存储在客户端上,我真的不想浪费更多带宽来传输冗余的 http 信息(例如浏览器类型、接受编码等)。在许多连接的过程中,这很快就会增加很多数据!
I would really like to just take my XMLHttpRequest and nuke all of the headers so that only the absolute minimum gets transmitted to the server. Is it possible to do this?
我真的很想使用我的 XMLHttpRequest 并取消所有标头,以便仅将绝对最小值传输到服务器。是否有可能做到这一点?
回答by Sripathi Krishnan
You have very little control over request headers, but you can still do a few things -
您对请求标头的控制很少,但您仍然可以做一些事情 -
- Reduce the size of the cookie. In general, you only want the session id, everything else can be eliminated and stored server side.
- Minimize http referrer by keeping a short URL. The longer your page url, the more data will have to be sent via the http referrer. One trick is to store data in the fragment identifier (the portion of the url after the #). The fragment identifier is never sent to the server, so you save a few bytes over there.
- Some request headers are only sent if you had previous set corresponding response headers. For example, you can indirectly control the ETag and if-modified-since request headers.
- 减小 cookie 的大小。一般来说,你只需要会话 id,其他一切都可以消除并存储在服务器端。
- 通过保持一个简短的 URL 来最小化 http 引用。您的页面 url 越长,必须通过 http 引荐来源网址发送的数据就越多。一种技巧是将数据存储在片段标识符(# 之后的 url 部分)中。片段标识符永远不会发送到服务器,因此您可以在那里保存几个字节。
- 某些请求标头仅在您之前设置了相应的响应标头时才会发送。例如,您可以间接控制 ETag 和 if-modified-since 请求标头。
You may want to consider Web Sockets. Support is pretty good (IE10+).
您可能需要考虑Web Sockets。支持非常好(IE10+)。
回答by Anomie
You may be able to override someof the standard headers using setRequestHeader()before sending the request, but it is possible the browser may not allow overriding of some and it seems there is no way to get a list of headers (besides asking the server to echo them back to you) to know which to try to override.
您可以在发送请求之前使用一些标准标头覆盖setRequestHeader(),但浏览器可能不允许覆盖某些标头,而且似乎无法获取标头列表(除了要求服务器回显它们)返回给您)以了解要尝试覆盖的内容。
回答by Ruslan Talpa
I think it's possible to remove all headers at least in some browsers. Take a look at the communication between gmail/calendar apps and the backend from google in chrome (it's not the same in firefox) it's possible google has some hidden api for the XMLHttpRequest object, you'll see something like the below output (notice there is no request headers section):
我认为至少在某些浏览器中可以删除所有标题。在 chrome 中查看 gmail/calendar 应用程序和来自 google 的后端之间的通信(在 firefox 中不一样)可能 google 有一些隐藏的 api 用于 XMLHttpRequest 对象,你会看到类似下面的输出(注意那里是没有请求标头部分):
Request URL:https://mail.google.com/mail/u/0/channel/bind?XXXXXXXXXXXXXX
Request Method:POST
Status Code:200 OK
Query String Parameters
OSID:XXXXXXXXXXXXX
OAID:XXXXXXXXX
VER:8
at:XXXXXXXXXXXXXX
it:30
SID:XXXXXXXXXXXX
RID:XXXXXXXXX
AID:XXXXXXXXXX
zx:XXXXXXXXXXXX
t:1
Request Payload
count=1&ofs=211&req0_type=cf&req0_focused=1&req0__sc=c
Response Headers
cache-control:no-cache, no-store, max-age=0, must-revalidate
content-encoding:gzip
content-type:text/plain; charset=utf-8
date:Tue, 09 Oct 2012 08:52:46 GMT
expires:Fri, 01 Jan 1990 00:00:00 GMT
pragma:no-cache
server:GSE
status:200 OK
version:HTTP/1.1
x-content-type-options:nosniff
x-xss-protection:1; mode=block

