我正在建设一个项目,并希望实施CORS。它安装在共享IIS服务器(共享主机)上。
在我的apphost.cs中,我根据我在web上找到的几篇文章启用和配置CORS。
Plugins.Add(new CorsFeature(
allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
allowedOrigins: "*",
allowCredentials: true,
allowedHeaders: "content-type, Authorization, Accept"));我还读到,在实现更新的SS (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;'
});我能够通过REST控制台(chrome )调用我的api,但是当我使用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我还没有对我的方法进行任何身份验证。
我看过这个问题并实现了它,如上面所示。这个相关的问题不是重复的。服务栈REST和CORS
我正在打的服务电话是.
public object Post(SelectedTroops request)
{
return new SelectedTroopsResponse { ArrayOfSelectedTroops = request.ArrayOfSelectedTroops };
}如果我遗漏了任何可能有帮助的东西,请告诉我。我想一个附带的问题可能是-你如何有效地跟踪和调试类似的东西?任何有用的提示都将不胜感激。
发布于 2013-09-26 17:28:12
你必须使用:
httpRes.EndRequest();这很重要,它是ServiceStack命名空间中的一个ServiceStack。我建议使用像ReSharper这样的工具,这使得自动查找和引用扩展方法变得非常简单。
https://stackoverflow.com/questions/19034514
复制相似问题