我試過下面這篇文章,但我還是不太有:使用WebAPI PUT/POST請求啓用CORS?
CORS support for PUT and DELETE with ASP.NET Web API
在我的web.config我有以下幾點:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<httpProtocol>
<customHeaders>
<!-- TODO: don't let anyone make requests - only approved clients -->
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="WebDAV" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
</system.webServer>
但在鉻,當我做POST請求我得到的不允許錯誤:
我的要求長相像這樣:
var request = $.ajax({
async: true,
url: apiEndpoint + 'api/login',
type: 'POST',
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: 'json'
})
apiEndpoint位於本地主機上,但位於不同的端口上 - 客戶端和api項目位於不同的解決方案中。
POST請求最終會進入服務器,但我總是收到與OPTIONS相關的錯誤,因此我從來沒有將cookie保存到客戶端。
我花了幾個小時試圖讓CORS用的WebAPI工作:
https://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API
但一些集版本的問題導致我在耍東西展現出來 - 希望有一個簡單的解決方案。
謝謝Badri。這讓我更進一步! – SB2055
你將如何在控制器中實現這一點? –
Imo POST不會創建預檢請求 –