php - 由于 CORS,Azure 网站上的 HTTP OPTIONS 请求失败

标签 php jquery azure cors azure-web-app-service

我最近将我们的服务器从 Rackspace CloudSites(在 Apache/Linux 上运行)迁移到 Windows Azure 网站。自迁移以来,由于 CORS,我们的 REST API 上的所有 jQuery AJAX 请求都开始失败。

我们使用自定义 header ,因此 jQuery 在运行实际 API 调用之前发出预检 HTTP OPTIONS 请求。问题是 OPTIONS 请求似乎没有到达我的 PHP 代码,而是由我似乎无法控制的其他实体(显然是 Web 服务器)返回。

我已经使用以下 header 几年了,所以我很确定问题不在 PHP 代码中:

<?php
    $this->output->set_header("Access-Control-Allow-Origin: *");
    $this->output->set_header("Access-Control-Allow-Methods: GET,POST,DELETE,HEAD,PUT,OPTIONS");
    $this->output->set_header("Access-Control-Allow-Headers: X-Olaround-Debug-Mode, Authorization, Accept");
    $this->output->set_header("Access-Control-Expose-Headers: X-Olaround-Debug-Mode, X-Olaround-Request-Start-Timestamp, X-Olaround-Request-End-Timestamp, X-Olaround-Request-Time, X-Olaround-Request-Method, X-Olaround-Request-Result, X-Olaround-Request-Endpoint" );
?>

我猜测该问题特定于 Azure 网站,因为该代码似乎在我的开发计算机(Windows 8/IIS 8.0)上也运行良好。我是 Azure(以及一般基于 Windows 的托管)的新手,因此我几乎不知道如何处理和调试此问题,因为 Azure 网站允许的控制非常少。

最佳答案

我决定发布一个完整的解决方案来解决这个问题,因为已经提供的答案(虽然技术上是正确的)在这种特殊情况下对我来说不起作用。诀窍是执行以下操作:

1。添加<customHeaders><httpProtocol>在 web.config 中

就像@hcoat上面也建议的那样,添加system.webServer.httpProtocol.customHeaders是解决问题的第一步(我之前已经自己尝试过,但没有成功)。在此处添加您需要为 CORS 设置的所有自定义 header 和 HTTP 方法。

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Olaround-Debug-Mode, Authorization, Accept" />
        <add name="Access-Control-Expose-Headers" value="X-Olaround-Debug-Mode, X-Olaround-Request-Start-Timestamp, X-Olaround-Request-End-Timestamp, X-Olaround-Request-Time, X-Olaround-Request-Method, X-Olaround-Request-Result, X-Olaround-Request-Endpoint" />
    </customHeaders>
</httpProtocol>

2。覆盖 PHP 的默认处理程序并删除 OPTIONSVerbHandler

下一步(@Bing Han 提供的解决方案)是删除默认的 OPTIONSVerbHandler在IIS中定义,并且还设置了自定义PHP54_via_FastCGI接受额外 HTTP 方法的处理程序。默认处理程序仅适用于 GET、POST 和 HEAD 请求。

<handlers>
    <remove name="OPTIONSVerbHandler" />
    <remove name="PHP54_via_FastCGI" />
    <add name="PHP54_via_FastCGI" path="*.php" verb="GET, PUT, POST, DELETE, HEAD, OPTIONS, TRACE, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK" modules="FastCgiModule" scriptProcessor="D:\Program Files (x86)\PHP\v5.4\php-cgi.exe" resourceType="Either" requireAccess="Script" />
</handlers>

看看this post for more details关于内部运作。

3。删除通过应用程序代码设置的所有响应 header

这是造成最多问题的最后一 block 拼图。由于 IIS 已经添加 <customHeaders> ,我在上面的问题中分享的 PHP 代码片段是重复的。这导致浏览器级别出现问题,无法很好地响应同一类型的多个 header 。

决赛web.config这适用于这个问题

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{R:1}" pattern="^(dir_path\.php|lolaround|lolaround\.php|app_assets)" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="lolaround.php/{R:1}" />
                </rule>
                <rule name="Imported Rule 2" stopProcessing="true">
                    <match url="lolaround/(.*)" ignoreCase="false" />
                    <action type="Rewrite" url="/lolaround.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,OPTIONS" />
                <add name="Access-Control-Allow-Headers" value="Origin, X-Olaround-Debug-Mode, Authorization, Accept" />
                <add name="Access-Control-Expose-Headers" value="X-Olaround-Debug-Mode, X-Olaround-Request-Start-Timestamp, X-Olaround-Request-End-Timestamp, X-Olaround-Request-Time, X-Olaround-Request-Method, X-Olaround-Request-Result, X-Olaround-Request-Endpoint" />
            </customHeaders>
        </httpProtocol>
        <handlers>
            <remove name="OPTIONSVerbHandler" />
            <remove name="PHP54_via_FastCGI" />
            <add name="PHP54_via_FastCGI" path="*.php" verb="GET, PUT, POST, HEAD, OPTIONS, TRACE, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK" modules="FastCgiModule" scriptProcessor="D:\Program Files (x86)\PHP\v5.4\php-cgi.exe" resourceType="Either" requireAccess="Script" />
        </handlers>
    </system.webServer>
</configuration>

注意:虽然@hcoat 和@Bing Han 的答案在这个问题上都很有用,但我只能将赏金授予其中之一。我决定将其交给 @Bing Han,因为他的回答让我最接近解决方案(而且我无法通过自己的搜索找到添加自定义 PHP 处理程序的方法)。

更新:我编辑了答案,添加了对 HTTP DELETE 方法的支持,而原始答案中缺少该方法。

关于php - 由于 CORS,Azure 网站上的 HTTP OPTIONS 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18981339/

相关文章:

php - 带有 MongoDB 子数组的 Codeigniter

php - 如何检查 PHP CURL SSL 是否有效

php - 将元素置于另一个元素的右侧

Javascript jQuery map 可重新加载

powershell - 新的 Azure 资源组 powershell

azure - Kubernetes Pod 不断重新启动(CrashBackoffLoop - 错误 : Request Time Out))

php - 如何使用 easyxdm 跨域自动调整 WordPress 页面内 iframe 的大小?

javascript - jQuery 与 AngularJS 与 Node.js 之间的区别

jquery - 没有 Jquery Mobile 或任何其他 UI 框架就无法创建 phonegap 应用程序

email - SendGrid 资源未更新