从 WCF REST Web 服务返回封装在回调函数中的 JSON
我有一个返回 JSON 的 Web 服务,但现在我想修改它以允许调用者指定回调函数,以便返回从: JSON DATA 到指定函数(JSON DATA); 我现在返回 JSON 的方式只是返回一个对象的实例并让 .NET 执行其序列化魔法,如果我更改为仅返回一个字符串,我可以添加函数名称和数据周围的括号,但是然后我最终在返回中加上引号,因为它是一个字符串,而我不想要它们。 那么我该怎么办呢?
这样做的原因是我们希望调用我们 API 的开发人员能够使用动态脚本标签,如下所述 http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html
I have a web service returning JSON, but now I'd like to modify it to allow callers to specify a callback function so the return goes from: JSON DATA to specifiedFunction(JSON DATA); The way I'm returning JSON right now is just by returning an instance of an object and having .NET do its serialization magic, if I change to just returning a string I can add the name of the function and the brackets around the data but then I end up with quotation marks in the return, because its a string and I don't want those. So how can I go about it?
Reason for this is we want developers calling our API to be able to use the dynamic script tag as explained here http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一个旧线程,但似乎每个人都坚持更改 WCF 以支持 JSONP,而这显然不是发帖者所要求的。 我在这里和forums.asp.net 发帖,但没有人花时间回复。
我最终将其作为流返回。 这会导致使用“原始”模式,并且 WCF 不会触及响应。 链接: http:// /blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx。
I know it's an old thread, but it seems that everyone insists on changing the WCF to support JSONP when that's clearly not what the poster asked. And I posted here and forums.asp.net and nobody took the time to reply.
I ended up returning it as a stream. This causes the "raw" mode to be used and WCF will not touch the response. Link: http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx.
我不知道他们使用的是哪个版本的 HTML,但在我使用过的所有版本中,脚本标记中的 src 属性必须是 URL。 他们以某种方式使用一个函数来代替,但我认为这不起作用。
您真的见过动态脚本标签的工作原理吗?
I don't know what version of HTML they're using, but in all the versions that I've used, the src attribute in a script tag has to be a URL. They're somehow using a function instead, and I don't see that working.
Have you actually seen a dynamic script tag work?
在 WCF 4.0 中添加了对 JSONP 的支持。 您可以使用
http://bendewey.wordpress .com/2009/11/24/using-jsonp-with-wcf-and-jquery/
In WCF 4.0 added support for JSONP. You may use
http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/
您所追求的技术称为 JSONP (带填充的 JSON)。
请参阅如何在 WCF 服务中支持 JSONP:
The technique you are after is called JSONP (JSON with Padding).
See How to support JSONP in WCF services: