从 WCF REST Web 服务返回封装在回调函数中的 JSON

发布于 2024-07-29 11:48:08 字数 420 浏览 1 评论 0原文

我有一个返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

粉红×色少女 2024-08-05 11:48:48

我知道这是一个旧线程,但似乎每个人都坚持更改 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.

黒涩兲箜 2024-08-05 11:48:42

我不知道他们使用的是哪个版本的 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?

江城子 2024-08-05 11:48:38

在 WCF 4.0 中添加了对 JSONP 的支持。 您可以使用

  <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
    </webHttpBinding>
  </bindings>

http://bendewey.wordpress .com/2009/11/24/using-jsonp-with-wcf-and-jquery/

In WCF 4.0 added support for JSONP. You may use

  <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
    </webHttpBinding>
  </bindings>

http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/

葬シ愛 2024-08-05 11:48:32

您所追求的技术称为 JSONP (带填充的 JSON)。

请参阅如何在 WCF 服务中支持 JSONP

JsonPEncoder 是一个包装编码器
在 WCF JSON 编码器上。 它代表
对包装编码器的大多数调用。 这
WriteMesage 方法已
覆盖以填充传出消息
使用回调方法。 这
JsonBehavior 用于服务上
启用 JSONP 编码的操作
那个操作。 查询参数为
保存回调的 URI
函数名称指定为
行为的 CallBack 属性。

The technique you are after is called JSONP (JSON with Padding).

See How to support JSONP in WCF services:

The JsonPEncoder is a wrapping encoder
on the WCF JSON encoder. It delegates
most calls to the wrapped encoder. The
WriteMesage methods have been
overriden to pad the outgoing message
with the callback method. The
JsonBehavior is used on the service
operation to enable JSONP encoding for
that operation. The query parameter of
the URI that holds the callback
function name is specified as the
CallBack property of the behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文