我正在構建一個項目並希望實施CORS。它安裝在共享的IIS服務器(共享主機)上。ServiceStack Cors
在我的apphost.cs中,根據我在網上找到的幾篇文章啓用和配置CORS。
Plugins.Add(new CorsFeature(
allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
allowedOrigins: "*",
allowCredentials: true,
allowedHeaders: "content-type, Authorization, Accept"));
我也看了,實施較新的SS API(IService),當我不得不爲的「選項」電話,所以我加了鉤......
this.RequestFilters.Add((httpReq, httpRes, requestDto) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.HttpMethod == "OPTIONS")
httpRes.EndRequest(); //add a 'using ServiceStack;'
});
我能打電話我的API通過REST控制檯(chrome插件),但是當我用ajax請求調用時,我得到了一個405錯誤。
Request URL:http://empire.thecodingpit.com/api/engine
Request Method:OPTIONS
Status Code:200 OK
Request Headers view source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:empire.thecodingpit.com
Origin:http://json.commublogs.com
Referer:http://json.commublogs.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Response Headers view source
Cache-Control:private
Content-Length:0
Date:Thu, 26 Sep 2013 17:00:59 GMT
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-MiniProfiler-Ids: ["f8c3ddb8434149feb689dd44d93bf862","6d0e7e0f8ac1456d98872a82af4d6602","67bdf08a19c641a7b26db0b43fd10888","1819b1ce3e314c0594ef9ecb1ac68fcf","7af8595373e345f3a9f8ade60c8a7817"]
X-Powered-By:ASP.NET
我對我的方法沒有任何[身份驗證] - 但。
我已經看過this question,並按照上圖所示實施了它。這個相關的問題不是重複的。 servicestack REST API and CORS
,我想提出的服務電話(POST)是...
public object Post(SelectedTroops request)
{
return new SelectedTroopsResponse { ArrayOfSelectedTroops = request.ArrayOfSelectedTroops };
}
如果我錯過什麼,可能會有幫助,請讓我知道。我想一個旁註問題可能是 - 你如何有效地跟蹤和調試這樣的事情?任何有用的提示將不勝感激。
mythz回答了一下,你也可以讀這個[question](http://stackoverflow.com/questions/18923930/sending-data-to-servicestack-restful-service-getting-access-is-denied/18927067# 18927067) – stefan2410