返回JSON包裹在一个回调函数从WCF REST Web服务回调、包裹、函数、JSON

2023-09-03 16:59:08 作者:暗恋你很久了/*

我有一个Web服务返回JSON,但现在我想修改它允许呼叫者指定一个回调函数,所以就返回来自:JSON数据来specifiedFunction(JSON数据);我返回JSON,现在的办法就是通过返​​回一个对象的实例并具有.NET做它的序列化的魔力,如果我改变只返回一个字符串,我可以添加函数的名称和周围的数据括号,但然后,我结束了在返回引号,因为它是一个字符串,我不希望这些。所以,我怎么能走呢?

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?

这样做的原因是,我们希望开发者调用我们的API,以便能够使用动态脚本标记如下解释http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html

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

推荐答案

你以后叫 JSONP的技术(JSON与填充)。

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

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

JsonPEn codeR 是一个包装连接codeR   在WCF JSON EN codeR。它代表   大多数调用包裹EN codeR。该    WriteMesage 方法已经   重写垫传出消息   与回调方法。该    JsonBehavior 上使用的服务   操作要启用JSONP编码   该操作。的查询参数   保存回调的URI   函数名被指定为   回调行为的属性。

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.