简体   繁体   English

CORS、IIS——有史以来最奇怪的失败

[英]CORS, IIS - the strangest failure ever

I got a Microsoft IIS server (over TMG firewall).我有一个 Microsoft IIS 服务器(通过 TMG 防火墙)。 I got Apache server (different location, no firewall).我有 Apache 服务器(不同的位置,没有防火墙)。 My IIS server serves data files (plain text, actually - CSV).我的 IIS 服务器提供数据文件(纯文本,实际上 - CSV)。

Apache server serves simple HTML page which loads data from IIS server via jQuery.ajax() method. Apache 服务器提供简单的 HTML 页面,该页面通过jQuery.ajax()方法从 IIS 服务器加载数据。 Or, XDomainRequest() on IE8 and maybe 9. (Made as $.ajax transport).或者,IE8 上的XDomainRequest()或者 9。(作为$.ajax传输)。

Here's my web.config for directory containing data:这是我的包含数据的目录的 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <location path=".">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Methods" value="GET,POST" />
          <add name="Access-Control-Allow-Headers" value="Content-Type" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>
</configuration>

Here's the test:这是测试:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CORS TEST</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script>
            $(function () {
                var h2 = $('h2:first');
                    h3 = $('h3:first');
                h3.text(navigator.userAgent);
                $.ajax({
                    url : '//my.iis.site.net/ajax/data/test.csv',
                    method : 'GET'
                })
                .done(function() {
                   h2.text('WORKS.'); 
                })
                .fail(function() {
                   h2.text('FAILED.'); 
                });
            });
        </script>
    </head>
    <body>
        <h1>CORS TEST:</h1>
        <h2></h2>
        <h3></h3>
    </body>
</html>

I tested it with all specified browsers supporting CORS.我使用所有支持 CORS 的指定浏览器对其进行了测试。 And a few more mobile ones.还有一些移动设备。 I couldn't make it fail.我不能让它失败。 It just works, all by the book.它只是有效,一切都在书中。

After testing this configuration with our team at work, we found it's OK to deliver our new site to a broader audience.在与我们的工作团队一起测试此配置后,我们发现可以将我们的新站点提供给更广泛的受众。 And it's when all hell broke loose.就在这时,一切都崩溃了。

Most of the people say it works.大多数人都说它有效。 But significant amount (definitely above 10%) of people say it doesn't work.但是相当多(绝对超过 10%)的人说它不起作用。 I'm afraid it's way more than 10%, it could be even 50%.恐怕超过10%,甚至有可能达到50%。

And here's the worst case: 2 persons, but almost (or totally) identical software (OS, configuration, programs, ISP, browsers).这是最糟糕的情况:2 个人,但几乎(或完全)相同的软件(操作系统、配置、程序、ISP、浏览器)。 One guy sees AJAX data, the other one doesn't.一个人看到 AJAX 数据,另一个人看不到。

Same OS, same browser (tested with 3 different browsers on both computers, exactly the same versions, and the same ISP).相同的操作系统,相同的浏览器(在两台计算机上使用 3 种不同的浏览器进行测试,完全相同的版本和相同的 ISP)。

On some computers CORS just doesn't work, no matter what!在某些计算机上,CORS 无论如何都不起作用!

And yes, they cleared all browser caches.是的,他们清除了所有浏览器缓存。 Didn't help.没有帮助。

Browser doesn't throw any error in console, $.ajax() method just triggers .fail() method, instead of .done() .浏览器不会在控制台中抛出任何错误, $.ajax()方法只是触发.fail()方法,而不是.done() I have no idea how to test it, since on my machine it always works.我不知道如何测试它,因为在我的机器上它总是有效。 Even on my all mobile devices and GSM network it works as charm.即使在我所有的移动设备和 GSM 网络上,它也很有魅力。 Even on quite old Android version and pretty old default browser.即使在相当旧的 Android 版本和相当旧的默认浏览器上。 No matter what I can't make it fail, just like some people just can't make it work on ANY of their computers or mobile devices.无论我做什么都不能让它失败,就像有些人不能让它在他们的任何计算机或移动设备上运行一样。

What evil magic is it?这是什么邪恶的魔法? Should I just forget about CORS and switch to JSONP transport?我应该忘记 CORS 并切换到 JSONP 传输吗?

There's a reason I don't use JSONP.我不使用 JSONP 是有原因的。 The data server will receive a huge traffic.数据服务器将收到巨大的流量。 The data is updated every 500ms and will be reloaded by thousands of users very often (even at 500ms rate).数据每 500 毫秒更新一次,并且会经常被成千上万的用户重新加载(即使以 500 毫秒的速度)。 Executing script on each request on my IIS server is not the best idea.在我的 IIS 服务器上对每个请求执行脚本并不是最好的主意。 It could have quite negative effect on performance.它可能会对性能产生相当负面的影响。

Any clues?有什么线索吗? Zip the data as PNG and unzip via JS?将数据压缩为 PNG 并通过 JS 解压缩? :) It's a crazy hack. :) 这是一个疯狂的黑客。 CORS is not crazy, but this is what I get. CORS 并不疯狂,但这就是我所得到的。

Any idea how could I start to test it?知道如何开始测试吗? Maybe it's a bug in jQuery, but I doubt it.也许这是 jQuery 中的一个错误,但我对此表示怀疑。 The guy which can't access the files via CORS can't access them even when requesting script was run from the same domain.即使请求脚本是从同一域运行的,无法通过 CORS 访问文件的人也无法访问它们。 I had to remove web.config to make AJAX work on the same domain.我必须删除 web.config 才能使 AJAX 在同一个域上工作。

If you place web.config file in a subdirectory of a web application, you MUST convert that subdirectory to an application in IIS Manager.如果将 web.config 文件放在 Web 应用程序的子目录中,则必须将该子目录转换为 IIS 管理器中的应用程序。 Otherwise it gives error 500 for every request to this subdirectory.否则,它会为对该子目录的每个请求提供错误 500。

Someone converted it to web application on one server, but not on the other one.有人在一台服务器上将其转换为 Web 应用程序,但在另一台服务器上却没有。 That's why some users got data, and some error 500 instead of data.这就是为什么一些用户得到数据,而一些错误 500 而不是数据。

Make sure that ur clients are not behind a proxy server or firewall.. some proxy servers or firewall may block or auto respond to preflight options request..确保您的客户端不在代理服务器或防火墙后面.. 某些代理服务器或防火墙可能会阻止或自动响应预检选项请求..

Also in the handler section of ur servers web.config file add同样在你的服务器 web.config 文件的处理程序部分添加

<handlers>
      <remove name="OPTIONSVerbHandler" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

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

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