2012-09-17 121 views
76

我最近遇到將Javascript請求發佈到另一個域。默認情況下,不允許將XHR發佈到其他域。在IIS7上啓用跨源資源共享

按照http://enable-cors.org/的說明,我在另一個域上啓用了這個功能。

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

enter image description here

現在一切工作正常,但它仍然是返回回送工作200響應之前405的響應。

Request URL:http://testapi.nottherealsite.com/api/Reporting/RunReport 
Request Method:OPTIONS 
Status Code:405 Method Not Allowed 
Request Headersview source 
Accept:*/* 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6 
Access-Control-Request-Headers:origin, content-type, accept 
Access-Control-Request-Method:POST 
Connection:keep-alive 
Host:testapi.nottherealsite.com 
Origin:http://test.nottherealsite.com 
Referer:http://test.nottherealsite.com/Reporting 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 
Response Headersview source 
Access-Control-Allow-Headers:Content-Type 
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS 
Access-Control-Allow-Origin:* 
Allow:POST 
Cache-Control:private 
Content-Length:1565 
Content-Type:text/html; charset=utf-8 
Date:Tue, 18 Sep 2012 14:26:06 GMT 
Server:Microsoft-IIS/7.5 
X-AspNet-Version:4.0.30319 
X-Powered-By:ASP.NET 

更新:2014年3月2日

有一個在MSDN雜誌最近更新的文章。詳圖CORS支持ASP.NET的Web API 2.

http://msdn.microsoft.com/en-us/magazine/dn532203.aspx

+0

它解決了我越來越的的jQuery插件bootgrid要加載glyphicons,半身人,regular.woff假排列圖標的問題,從引導Fonts文件夾 –

回答

65

這是可能的IIS 7「處理」的HTTP OPTIONS響應,而不是你的應用程序指定它的情況。要確定這一點,在IIS7中,

  1. 轉到您網站的處理程序映射。

  2. 向下滾動到'OPTIONSVerbHandler'。

  3. 改變 'ProtocolSupportModule' 到 'IsapiHandler'

  4. 集的可執行文件: %WINDIR%\ Microsoft.NET \框架\ v4.0.30319 \ ASPNET_ISAPI.DLL

現在,當發送一個HTTP OPTIONS動詞時,上面的配置條目應該會啓動。

或者,您可以在BeginRequest方法中響應HTTP OPTIONS謂詞。

protected void Application_BeginRequest(object sender,EventArgs e) 
    { 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 

     if(HttpContext.Current.Request.HttpMethod == "OPTIONS") 
     { 
      //These headers are handling the "pre-flight" OPTIONS call sent by the browser 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
      HttpContext.Current.Response.End(); 
     } 

    } 
+4

我都嘗試的方法,但只的BeginRequest方法爲我工作。謝謝@Shah –

+0

對我來說,以與OP相同的方式將它添加到web.config是唯一的方法。 Global.asax/BeginRequest不起作用。 – twDuke

+4

經過2天的研究,使用基於Application_BeginRequest的替代解決方案是我解決問題的唯一方法。我嘗試了其他方法,使用'customHeaders'(http://stackoverflow.com/a/19091291/827168),刪除'OPTIONSVerbHandler'處理程序,刪除'WebDAV'模塊和處理程序(http://stackoverflow.com/a/20705500/ 827168)但沒有爲我工作。希望這會幫助別人。並感謝@Mendhak的答案! – pomeh

9

的405響應是 「不允許的方法」 的迴應。這聽起來像你的服務器沒有正確配置來處理CORS預檢請求。您需要做兩件事:

1)使IIS7能夠響應HTTP OPTIONS請求。您正在獲得405,因爲IIS7拒絕了OPTIONS請求。我不知道如何做到這一點,因爲我不熟悉IIS7,但Stack Overflow上可能有其他人。

2)配置您的應用程序以響應CORS預檢請求。您可以通過添加以下兩行的Access-Control-Allow-Origin線下方的<customHeaders>部分做到這一點:

<add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE" /> 
<add name="Access-Control-Allow-Headers" value="Content-Type" /> 

您可能需要添加其它的值基於什麼標頭的要求是要求的Access-Control-Allow-Headers部分。你有提供請求的示例代碼嗎?

您可以瞭解更多關於CORS和CORS預檢這裏:http://www.html5rocks.com/en/tutorials/cors/

15

我發現在http://help.infragistics.com/Help/NetAdvantage/jQuery/2013.1/CLR4.0/html/igOlapXmlaDataSource_Configuring_IIS_for_Cross_Domain_OLAP_Data.html找到的信息是非常有幫助在IIS 7中

的WCF服務建立HTTP OPTIONS添加以下到我的web.config,然後在移動OPTIONSVerbHandler IIS 7'hander mappings'列表位於列表的頂部。我還通過雙擊處理程序映射部分中的手柄,然後在「請求限制」中單擊訪問選項卡,給OPTIONSVerbHander讀取權限。

不幸的是,我很快發現IE似乎不支持將標題添加到它們的XDomainRequest對象(將Content-Type設置爲text/xml並添加SOAPAction標頭)。

只是想分享這一點,因爲我花了一天的更好的一部分尋找如何處理它。

<system.webServer> 
    <httpProtocol> 
     <customHeaders> 
      <add name="Access-Control-Allow-Origin" value="*" /> 
      <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" /> 
      <add name="Access-Control-Allow-Headers" value="Content-Type, soapaction" /> 
     </customHeaders> 
    </httpProtocol> 
</system.webServer> 
+0

Shah的IIS isapiHandler配置沒有工作,但我沒有嘗試編程方式。不過,我也發現web.config中的上述WCF配置有竅門。 Chrome仍然會發送OPTIONS並且第一次獲得405,但是它會發送另一個請求,這是正確的。 – Marshal

+0

它看起來像當前版本的Chrome不再考慮在具有錯誤http狀態的響應中收到的cors頭部。所以在這種情況下不會再繼續進行跨域請求。這是一個更好的行爲imho。 –

+0

謝謝你的迴應。如果您瀏覽了上面的回覆,請不要忘記按上面所述提供OPTIONSVerbHander讀取權限。 –

21

我不能發表評論,所以我必須把這個在一個單獨的答案,但它涉及到沙阿接受的答案。

我最初通過在IIS中配置OPTIONSVerbHandler來遵守Shahs答案(謝謝!),但是當我重新部署我的應用程序時,我的設置已經恢復。

我最終刪除了我的Web.config中的OPTIONSVerbHandler。

<handlers> 
    <remove name="OPTIONSVerbHandler"/> 
</handlers> 
+12

對於那些每週沒有使用web.config的人來說, 「」 – SemanticZen

6

DavidG answer這實在是接近的東西需要一個基本的解決方案闡述:

  • 首先,配置OPTIONSVerbHandler淨處理程序之前執行。

    1. 在IIS控制檯中,選擇「處理程序映射」(無論是在服務器級別或站點級別;提防在現場級,將重新定義爲您的網站所有的處理程序和忽略後在服務器級別上進行的任何變化;當然在服務器級別,如果他們需要自己處理選項動詞,這可能會破壞其他網站)。
    2. 在操作窗格中,選擇「查看有序列表...」查找OPTIONSVerbHandler,並將其向上移動(大量點擊...)。

    您還可以通過以下<system.webServer><handlers>重新定義的所有處理做到這一點在web.config中(<clear>然後<add ...>他們回來了,這是什麼呢IIS控制檯你)(順便說一下,有沒有必要要求「讀」這個處理程序許可)

  • 其次,配置自定義HTTP標頭爲您CORS需求,如:

    <system.webServer> 
        <httpProtocol> 
        <customHeaders> 
         <add name="Access-Control-Allow-Origin" value="*"/> 
         <add name="Access-Control-Allow-Headers" value="Content-Type"/> 
         <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS"/> 
        </customHeaders> 
        </httpProtocol> 
    </system.webServer> 
    

    你也可以做到這一點在IIS控制檯。

這是一個基本的解決方案,因爲它甚至可以根據要求發送cors頭,不需要它。但是對於WCF,它看起來是最簡單的一個。使用MVC或webapi,我們可以通過代碼(「手動」或通過最新版本的webapi中提供的內置支持)來代替處理OPTIONS動詞和cors頭文件。

+0

這對我有用。我不喜歡每個處理程序都在Web.config中重新定義,但是,似乎是我需要做的,以使我們的WCF服務CORS啓用。 +1 – Manny

2

使用ASP.net Web API 2通過nuget安裝Microsoft ASP.NET Cross Origin支持。

http://enable-cors.org/server_aspnet.html

public static void Register(HttpConfiguration config) 
{ 
var enableCorsAttribute = new EnableCorsAttribute("http://mydomain.com", 
                "Origin, Content-Type, Accept", 
                "GET, PUT, POST, DELETE, OPTIONS"); 
     config.EnableCors(enableCorsAttribute); 
} 
+0

你知道是否有'programmatically'設置'EnableCorsAttribute'的第一個「ORIGIN」參數嗎?讓我們來說說註冊表中的不是這裏,而是針對每個單獨的HTTP請求。檢測它來自的源,確保它是正確的(檢查一些已批准的請求)然後返回一個響應,其中包含Access-Allow-Control -Origin =「thatdomain.com」'? @安德魯 –

1

Alsalaam Aleykum。

第一種方法是按照在此連結的指示:

http://help.infragistics.com/Help/NetAdvantage/jQuery/2013.1/CLR4.0/html/igOlapXmlaDataSource_Configuring_IIS_for_Cross_Domain_OLAP_Data.html

,其對應於這些配置:

<handlers> 
 
    <clear /> 
 
    <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" type="" modules="ProtocolSupportModule" scriptProcessor="" resourceType="Unspecified" requireAccess="Read" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" /> 
 
    <add name="xamlx-ISAPI-4.0_64bit" path="*.xamlx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="4194304" /> 
 
    <add name="xamlx-ISAPI-4.0_32bit" path="*.xamlx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="4194304" /> 
 
    <add name="xamlx-Integrated-4.0" path="*.xamlx" verb="GET,HEAD,POST,DEBUG" type="System.Xaml.Hosting.XamlHttpHandlerFactory, System.Xaml.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" scriptProcessor="" 
 
    resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="rules-ISAPI-4.0_64bit" path="*.rules" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="rules-ISAPI-4.0_32bit" path="*.rules" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="rules-Integrated-4.0" path="*.rules" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" 
 
    scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="xoml-ISAPI-4.0_64bit" path="*.xoml" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="xoml-ISAPI-4.0_32bit" path="*.xoml" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="xoml-Integrated-4.0" path="*.xoml" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" 
 
    scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="svc-ISAPI-4.0_64bit" path="*.svc" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="svc-ISAPI-4.0_32bit" path="*.svc" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="svc-Integrated-4.0" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" scriptProcessor="" 
 
    resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="ISAPI-dll" path="*.dll" verb="*" type="" modules="IsapiModule" scriptProcessor="" resourceType="File" requireAccess="Execute" allowPathInfo="true" preCondition="" responseBufferLimit="4194304" /> 
 
    <add name="AXD-ISAPI-4.0_64bit" path="*.axd" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="PageHandlerFactory-ISAPI-4.0_64bit" path="*.aspx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="SimpleHandlerFactory-ISAPI-4.0_64bit" path="*.ashx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="WebServiceHandlerFactory-ISAPI-4.0_64bit" path="*.asmx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="HttpRemotingHandlerFactory-rem-ISAPI-4.0_64bit" path="*.rem" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="HttpRemotingHandlerFactory-soap-ISAPI-4.0_64bit" path="*.soap" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="aspq-ISAPI-4.0_64bit" path="*.aspq" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness64" 
 
    responseBufferLimit="0" /> 
 
    <add name="cshtm-ISAPI-4.0_64bit" path="*.cshtm" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="cshtml-ISAPI-4.0_64bit" path="*.cshtml" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="vbhtm-ISAPI-4.0_64bit" path="*.vbhtm" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="vbhtml-ISAPI-4.0_64bit" path="*.vbhtml" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="TraceHandler-Integrated-4.0" path="trace.axd" verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TraceHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="WebAdminHandler-Integrated-4.0" path="WebAdmin.axd" verb="GET,DEBUG" type="System.Web.Handlers.WebAdminHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="AssemblyResourceLoader-Integrated-4.0" path="WebResource.axd" verb="GET,DEBUG" type="System.Web.Handlers.AssemblyResourceLoader" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="PageHandlerFactory-Integrated-4.0" path="*.aspx" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.SimpleHandlerFactory" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="WebServiceHandlerFactory-Integrated-4.0" path="*.asmx" verb="GET,HEAD,POST,DEBUG" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" modules="ManagedPipelineHandler" 
 
    scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="HttpRemotingHandlerFactory-rem-Integrated-4.0" path="*.rem" verb="GET,HEAD,POST,DEBUG" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
 
    modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="HttpRemotingHandlerFactory-soap-Integrated-4.0" path="*.soap" verb="GET,HEAD,POST,DEBUG" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory, System.Runtime.Remoting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
 
    modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="aspq-Integrated-4.0" path="*.aspq" verb="GET,HEAD,POST,DEBUG" type="System.Web.HttpForbiddenHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="cshtm-Integrated-4.0" path="*.cshtm" verb="GET,HEAD,POST,DEBUG" type="System.Web.HttpForbiddenHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="cshtml-Integrated-4.0" path="*.cshtml" verb="GET,HEAD,POST,DEBUG" type="System.Web.HttpForbiddenHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="vbhtm-Integrated-4.0" path="*.vbhtm" verb="GET,HEAD,POST,DEBUG" type="System.Web.HttpForbiddenHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="vbhtml-Integrated-4.0" path="*.vbhtml" verb="GET,HEAD,POST,DEBUG" type="System.Web.HttpForbiddenHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" 
 
    responseBufferLimit="4194304" /> 
 
    <add name="ScriptHandlerFactoryAppServices-Integrated-4.0" path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" modules="ManagedPipelineHandler" 
 
    scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="ScriptResourceIntegrated-4.0" path="*ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" modules="ManagedPipelineHandler" 
 
    scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="4194304" /> 
 
    <add name="AXD-ISAPI-4.0_32bit" path="*.axd" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="PageHandlerFactory-ISAPI-4.0_32bit" path="*.aspx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="SimpleHandlerFactory-ISAPI-4.0_32bit" path="*.ashx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="WebServiceHandlerFactory-ISAPI-4.0_32bit" path="*.asmx" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="HttpRemotingHandlerFactory-rem-ISAPI-4.0_32bit" path="*.rem" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="HttpRemotingHandlerFactory-soap-ISAPI-4.0_32bit" path="*.soap" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" 
 
    allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="aspq-ISAPI-4.0_32bit" path="*.aspq" verb="*" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="classicMode,runtimeVersionv4.0,bitness32" 
 
    responseBufferLimit="0" /> 
 
    <add name="cshtm-ISAPI-4.0_32bit" path="*.cshtm" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="cshtml-ISAPI-4.0_32bit" path="*.cshtml" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="vbhtm-ISAPI-4.0_32bit" path="*.vbhtm" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="vbhtml-ISAPI-4.0_32bit" path="*.vbhtml" verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="TRACEVerbHandler" path="*" verb="TRACE" type="" modules="ProtocolSupportModule" scriptProcessor="" resourceType="Unspecified" requireAccess="None" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" /> 
 
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 
 
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG" type="" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 
 
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" modules="ManagedPipelineHandler" scriptProcessor="" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" 
 
    preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" /> 
 
    <add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" requireAccess="Read" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" 
 
    /> 
 
</handlers>

的第二種方式是,以在您的BeginRequest方法中響應HTTP OPTIONS動詞。

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
    { 
     HttpContext.Current.Response.AddHeader("Access-Control-Request-Method", "GET ,POST, PUT, DELETE"); 

     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin,Content-Type, Accept"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "86400"); // 24 hours 
     HttpContext.Current.Response.End(); 
    } 
} 
3

對我來說,解決辦法是補充:

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="WebDAVModule"/> 
    </modules> 
</system.webServer> 

爲了我的web.config

0

一個還沒有在這些答覆中提到的事情是,如果你使用的是IIS,並有子應用程序與他們自己的單獨的web.config,您可能需要在包含以下代碼的父目錄中有一個web.config。

<httpProtocol> 
    <customHeaders> 
    <add name="Access-Control-Allow-Origin" value="*"/> 
    <add name="Access-Control-Allow-Headers" value="Content-Type"/> 
    <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS"/> 
    </customHeaders> 
</httpProtocol>