无法让 JSONP 与 WCF 数据服务一起使用
从我阅读和观看的所有内容看来,从 WCF 数据服务公开 JSON 应该像向服务类添加 JSONPSupportBehavior 属性一样简单。
问题是 VS2010 无法识别 JSONPSupportBehavior
属性。
我缺少参考吗?从所有文章来看,它似乎都是开箱即用的。
It seems from all that I read and watched, exposing JSON from a WCF Data Service should be as easy as adding the JSONPSupportBehavior
attribute to the service class.
The problem is that VS2010 doesn't recognize the JSONPSupportBehavior
attribute.
Is there a reference I am missing? It seems like from all the articles, it was supported out of the box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WCF 数据服务支持开箱即用的 JSON,无需添加属性或任何内容。
为了接收 JSON 格式的响应,客户端使用标准 HTTP 内容类型协商。
具体来说,他们需要在请求的
accept
标头中包含application/json
。OData 协议文档的本节中有一些示例。
这些示例显示原始 HTTP 请求。不同的客户端 HTTP API 有不同的机制来指定请求标头。
JSONPSupportBehavior
属性是一个扩展示例,除了内容类型协商之外,它还允许客户端使用 URL 查询字符串选项(即$format=json
),并且添加了对“JSONP”的支持(即$callback=[function-name]
)。这些在您无法控制标头的情况下非常有用,例如通过脚本标记进行跨域访问时。
如果您想使用
JSONPSupportBehavior
,您可以在此处获取它:http:// code.msdn.microsoft.com/DataServicesJSONP
WCF Data Services supports JSON out of the box, no need to add attributes or anything.
In order to receive a response in JSON format clients use standard HTTP content type negotiation.
Specifically, they need to include
application/json
in theaccept
header of the request.There are examples in this section of the OData protocol documentation.
These examples show the raw HTTP requests. Different client HTTP APIs have different mechanisms to specify request headers.
The
JSONPSupportBehavior
attribute is an example of an extension that allows clients to use a URL query string option (i.e.$format=json
) in addition to content-type negotiation, and also adds support for "JSONP" (i.e.$callback=[function-name]
).These are useful in situations where you don't control the headers, such as when doing cross-domain access through script tags.
If you want to use the
JSONPSupportBehavior
you can obtain it here:http://code.msdn.microsoft.com/DataServicesJSONP
较新版本的 WCF 数据服务默认支持 JSON,并且您必须
在请求标头中包含 JSON。
已经不够了。更多信息 此处。
Newer versions of WCF Data Services support JSON by default and you must have
in the request header.
is no longer sufficient. More info here.
WCF 不仅支持开箱即用的 JSON,如果您使用 WCF WebApi(预览版 6),它还支持开箱即用的 JSONP。使用 NuGet 进入 VS 2010。安装完成后,请查看此处了解更多信息。
WCF not only suppors JSON out-of-the-box, but it also supports JSONP out-of-the-box if you use WCF WebApi (preview 6). Get in VS 2010 with NuGet. Once you have it installed, look here for more information.