我想使用基于 ajax 的组件 (KendoUI) 来读取/修改 WCF DataServices 实现的 OData 端点上的实体。
首先,服务实现相当简单:
public class MyFooService : DataService<FooContext>
{
public static void SetEntitySetAccessRules(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Foos", EntitySetRights.AllWrite);
}
}
现在我期望能够使用 PUT 修改实体。 KendoUI 提供了一个漂亮且简单的配置界面,并且在生成 PUT 请求方面做得很好。
我们正在发出跨域请求并使用 CORS。因此,例如,Firefox 在发送 PUT 之前向 OData 服务发送预检 OPTIONS 请求。
不幸的是,服务端点似乎不支持开箱即用的 OPTIONS:对 OPTIONS 请求的响应是“501 Not Implemented”,内容为空。至少我们设法使响应具有如下 CORS 标头:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!-- Enable cross-origin resource sharing -->
<!-- http://enable-cors.org/#how-asp.net -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, PUT, DELETE, GET, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="content-Type, accept, origin, X-Requested-With" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
谷歌搜索这个有点具有挑战性,因为“选项”是一个非常流行的术语......
I found 本文 http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf.aspx但看起来很、very复杂的。我的意思是,OData 就是 REST,我无法想象 WCF 数据服务不提供允许预检请求的简单方法,或者?