从WCF REST Web服务返回包装在回调函数中的JSON [英] Return JSON wrapped in a callback function from a WCF REST web service

查看:378
本文介绍了从WCF REST Web服务返回包装在回调函数中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web服务返回JSON,但现在我想修改它允许调用者指定一个回调函数,所以返回值从:JSON DATA到specifiedFunction(JSON DATA);我现在返回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 (带Padding的JSON)。

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

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


JsonPEncoder 是一个包装编码器
on WCF JSON编码器。它将
多数调用委托给包装的编码器。
WriteMesage 方法已被
覆盖以使用回调方法填充传出消息

JsonBehavior 用于服务
操作以对
启用JSONP编码操作。
的查询参数保存回调
函数名的URI被指定为行为的
CallBack 属性。

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.

这篇关于从WCF REST Web服务返回包装在回调函数中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆