压缩 jQuery 的 Web 服务响应
我正在尝试对来自 ASMX Web 服务的 JSON 响应进行 gzip 压缩,以供 jQuery 在客户端使用。
我的 web.config 已经设置了 httpCompression,如下所示:(我正在使用 IIS 7)
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
staticCompressionDisableCpuUsage="90" staticCompressionEnableCpuUsage="60"
dynamicCompressionDisableCpuUsage="80" dynamicCompressionEnableCpuUsage="50">
<dynamicTypes>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="text/css" enabled="true"/>
<add mimeType="video/x-flv" enabled="true"/>
<add mimeType="application/x-shockwave-flash" enabled="true"/>
<add mimeType="text/javascript" enabled="true"/>
<add mimeType="text/*" enabled="true"/>
<add mimeType="application/json; charset=utf-8" enabled="true"/>
</dynamicTypes>
<staticTypes>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="text/css" enabled="true"/>
<add mimeType="video/x-flv" enabled="true"/>
<add mimeType="application/x-shockwave-flash" enabled="true"/>
<add mimeType="text/javascript" enabled="true"/>
<add mimeType="text/*" enabled="true"/>
</staticTypes>
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
</httpCompression>
<urlCompression doDynamicCompression="true" doStaticCompression="true"/>
通过 fiddler 我可以看到正常的 aspx 和其他压缩工作正常。然而,jQuery ajax 请求和响应按其应有的方式工作,只是没有任何内容被压缩。
我缺少什么?
I'm attempting to gzip a JSON response from an ASMX web service to be consumed on the client-side by jQuery.
My web.config already has httpCompression set like so: (I'm using IIS 7)
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
staticCompressionDisableCpuUsage="90" staticCompressionEnableCpuUsage="60"
dynamicCompressionDisableCpuUsage="80" dynamicCompressionEnableCpuUsage="50">
<dynamicTypes>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="text/css" enabled="true"/>
<add mimeType="video/x-flv" enabled="true"/>
<add mimeType="application/x-shockwave-flash" enabled="true"/>
<add mimeType="text/javascript" enabled="true"/>
<add mimeType="text/*" enabled="true"/>
<add mimeType="application/json; charset=utf-8" enabled="true"/>
</dynamicTypes>
<staticTypes>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="text/css" enabled="true"/>
<add mimeType="video/x-flv" enabled="true"/>
<add mimeType="application/x-shockwave-flash" enabled="true"/>
<add mimeType="text/javascript" enabled="true"/>
<add mimeType="text/*" enabled="true"/>
</staticTypes>
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
</httpCompression>
<urlCompression doDynamicCompression="true" doStaticCompression="true"/>
Through fiddler I can see that normal aspx and other compressions work fine. However, the jQuery ajax request and response work as they should, only nothing gets compressed.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
web.config
中的更改不起作用,因为applicationHost.config
中存在以下行:如果将其替换为:
则可以在本地进行更改。
我认为这更方便,因为您可以以不同的方式配置每个服务,并且如果必须添加新的 MIME 类型,则无需编辑
applicationHost.config
。以下是如何在位于
service
子文件夹中的单个 ASMX 服务上的web.config
中激活压缩的示例:关于
applicationHost.config
的实际编辑code>,我怀疑它不是文件系统中的真实文件。如果您将该文件复制到桌面上,您将能够使用任何文本编辑器对其进行编辑,然后将其复制回其原始文件夹。Changes in
web.config
don't work because of the following line inapplicationHost.config
:If you replace it by:
the changes are possible locally.
I think this is more convenient as you are able to configure every service differently and you don't have to edit your
applicationHost.config
if you must add a new MIME type.Here is an example how to activate compression in
web.config
on a single ASMX service located in theservice
subfolder:Concerning the actual editing of
applicationHost.config
, I suspect that it is not a true file in the file system. If you copy that file on your desktop, you will be able to edit it with any text editor, then copy it back to its original folder.http://forums.iis.net/t/1160210.aspx?missing +应用程序主机+配置
有关缺少 applicationhost.config 的一些解释,用于将 overrideModeDefault 属性更改为 Allow。这是由于 SYSWOW32 重定向造成的。
此外,您可能看不到配置文件,除非您
这是因为由于某种原因,某些 64 位编辑器仍然以某种方式使用有错误的文件选择器对话框。
http://forums.iis.net/t/1160210.aspx?missing+applicationhost+config
A bit of explanation regarding the missing applicationhost.config to change overrideModeDefault attribute to Allow. It's due to SYSWOW32 redirection.
Also you may not see the configuration files until you
This is because for some reason some 64bit editors still use a faulty file-chooser dialog somehow.
您只能在 applicationHost.config 中更改 httpCompression。
请参阅此链接
像您一样,我尝试在
web.config中更改它
首先,但没有成功。仅当我将以下行添加到C:\Windows\System32\inetsrv\config\applicationHost.config
时,它才起作用:You can change httpCompression only in applicationHost.config.
See this link
Like you, I tried changing it in
web.config
first, but it didn't work. It worked only when I added the following lines toC:\Windows\System32\inetsrv\config\applicationHost.config
:请使用记事本编辑 applicationHost.config。 我已经浪费了几个小时才明白我在记事本++(以及 Visual Studio 2010 编辑器!!)中所做的更改没有被 IIS 应用。
将额外的 mimeType 添加到 dynamicTypes/staticTypes 集合中的替代方法是使用 appcmd。
"C:\Windows\System32\Inetsrv\Appcmd.exe" set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/javascript',enabled='True']" /commit :apphost
再说一遍:进行这些更改后 - 您只能在记事本中看到它们。 Notepad++(以及 Visual Studio 2010 编辑器!!)为 applicationHost.config 维护某种该死的替代现实/存储。即使在记事本中编辑文件并在 np++/VS 中重新打开文件后,它也会向您显示他自己的文件版本(与您在记事本中看到的版本不同)。
DO USE NOTEPAD to edit applicationHost.config. I've wasted several hours before understood that my changes made in notepad++ (as well as in Visual Studio 2010 editor!!) aren't applied by IIS.
Alternative way to add additional mimeType into dynamicTypes/staticTypes collection is to use appcmd.
"C:\Windows\System32\Inetsrv\Appcmd.exe" set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/javascript',enabled='True']" /commit:apphost
And again: after these changes made - you'll see them only in notepad. Notepad++ (as well as Visual Studio 2010 editor!!) maintains some kind of f*ing alternate reality/storage for applicationHost.config. It shows you his own version of the file (different from the one you see in notepad) even after the file edited in notepad and reopened in np++/VS.
Eric P 的答案大部分是正确的...您需要完全匹配 IIS 在其 HTTP 响应标头中发送的 Content-Type 标头。由于某种原因,我们的 IIS7 服务器响应如下:
内容类型:application/json; q=0.5
我以前从未观察到服务器响应中的质量因数。多么奇怪啊。
当我们将其添加到 .config 文件中的动态类型时,一切都开始工作:
Eric P's answer is mostly correct... you need to EXACTLY match the Content-Type header sent by IIS in its HTTP Response Headers. For some reason our IIS7 server was responding with:
Content-Type: application/json; q=0.5
I had never ever observed a quality factor in a server response before. How bizarre.
When we added this to the dynamicTypes in the .config file everything started working: