简体   繁体   English

压缩jQuery的Web服务响应

[英]Compressing a web service response for jQuery

I'm attempting to gzip a JSON response from an ASMX web service to be consumed on the client-side by jQuery. 我试图通过jQuery从客户端使用ASMX Web服务gzip JSON响应。

My web.config already has httpCompression set like so: (I'm using IIS 7) 我的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"/>

Through fiddler I can see that normal aspx and other compressions work fine. 通过提琴手我可以看到正常的aspx和其他按压工作正常。 However, the jQuery ajax request and response work as they should, only nothing gets compressed. 但是,jQuery ajax请求和响应应该工作,只有没有得到压缩。

What am I missing? 我错过了什么?

You can change httpCompression only in applicationHost.config . 您只能在applicationHost.config中更改httpCompression See this link 看到这个链接

Like you, I tried changing it in web.config first, but it didn't work. 和你一样,我尝试先在web.config更改它,但它没有用。 It worked only when I added the following lines to C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config : 它只在我C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config添加到C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config时才有效:

  <dynamicTypes>
       ...
       <add mimeType="application/json" enabled="true" />
       <add mimeType="application/json; charset=utf-8" enabled="true" />
       ...
  </dynamicTypes>

DO USE NOTEPAD to edit applicationHost.config. 请使用NOTEPAD编辑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. 我已经浪费了几个小时才明白我在notepad ++(以及Visual Studio 2010编辑器!!)中所做的更改不是由IIS应用的。

Alternative way to add additional mimeType into dynamicTypes/staticTypes collection is to use appcmd. 将其他mimeType添加到dynamicTypes / staticTypes集合的另一种方法是使用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. Notepad ++(以及Visual Studio 2010编辑器!!)为applicationHost.config维护某种f * ing备用现实/存储。 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. 它显示了他自己的文件版本(与你在记事本中看到的版本不同),即使在记事本中编辑文件并在np ++ / VS中重新打开后也是如此。

Changes in web.config don't work because of the following line in applicationHost.config : 由于applicationHost.config中的以下行, web.config更改不起作用:

<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />

If you replace it by: 如果您将其替换为:

<section name="httpCompression" overrideModeDefault="Allow" />

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. 我认为这更方便,因为您可以以不同的方式配置每个服务,如果必须添加新的MIME类型,则不必编辑applicationHost.config

Here is an example how to activate compression in web.config on a single ASMX service located in the service subfolder: 以下是如何在位于service子文件夹中的单个ASMX服务上的web.config激活压缩的示例:

<location path="service/MySpecificWebService.asmx">
    <system.webServer>
        <httpCompression>
            <dynamicTypes>
                <add mimeType="application/json" enabled="true" />
                <add mimeType="application/json; charset=utf-8" enabled="true" />
            </dynamicTypes>
        </httpCompression>
        <urlCompression doDynamicCompression="true" />
    </system.webServer>
</location>

Concerning the actual editing of applicationHost.config , I suspect that it is not a true file in the file system. 关于applicationHost.config的实际编辑,我怀疑它不是文件系统中的真实文件。 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. 如果您在桌面上复制该文件,则可以使用任何文本编辑器对其进行编辑,然后将其复制回原始文件夹。

Eric P's answer is mostly correct... you need to EXACTLY match the Content-Type header sent by IIS in its HTTP Response Headers. Eric P的答案大多是正确的......您需要在其HTTP响应标头中完全匹配IIS发送的Content-Type标头。 For some reason our IIS7 server was responding with: Content-Type: application/json; 出于某种原因,我们的IIS7服务器响应:Content-Type:application / json; q=0.5 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: 当我们将它添加到.config文件中的dynamicTypes时,一切都开始工作:

  <dynamicTypes>
       ...
       <add mimeType="application/json" enabled="true" />
       <add mimeType="application/json; q=0.5" enabled="true" />
       <add mimeType="application/json; charset=utf-8" enabled="true" />
       <add mimeType="application/json; q=0.5; charset=utf-8" enabled="true" />
       ...
  </dynamicTypes>

http://forums.iis.net/t/1160210.aspx?missing+applicationhost+config http://forums.iis.net/t/1160210.aspx?missing+applicationhost+config

The config file is supposed to be %windir%\\system32\\inetsrv\\config\\applicationhost.config. 配置文件应该是%windir%\\ system32 \\ inetsrv \\ config \\ applicationhost.config。

(Note that if your application (which is searching for applicationhost.config) is a 32bit app (for example, if you're using a 32bit CMD.EXE), then you won't be able to see the configuration file due to Windows SYSWOW32 redirection) (请注意,如果您的应用程序(搜索applicationhost.config)是32位应用程序(例如,如果您使用的是32位CMD.EXE),那么由于Windows,您将无法看到配置文件SYSWOW32重定向)

A bit of explanation regarding the missing applicationhost.config to change overrideModeDefault attribute to Allow . 关于缺少applicationhost.config以将overrideModeDefault属性更改为Allow的一些解释 It's due to SYSWOW32 redirection. 这是由于SYSWOW32重定向。

Also you may not see the configuration files until you 您也可能看不到配置文件

  1. Open the folder by pasting "%windir%\\system32\\inetsrv\\config\\" exactly in the File Explorer location bar, and not your text editor 通过在文件资源管理器位置栏中准确粘贴“%windir%\\ system32 \\ inetsrv \\ config \\”打开文件夹,而不是文本编辑器
  2. Right-click and edit the file directly in that folder. 右键单击并直接在该文件夹中编辑该文件。

This is because for some reason some 64bit editors still use a faulty file-chooser dialog somehow. 这是因为出于某种原因,某些64位编辑器仍以某种方式使用错误的文件选择器对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM