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?
我有一个返回JSON的Web服务,但现在我想修改它以允许调用者指定一个回调函数,所以返回从:JSON DATA到specifiedFunction(JSON DATA);我现在返回JSON的方式只是返回一个对象的实例并让.NET执行其序列化魔术,如果我改为只返回一个字符串我可以添加函数的名称和数据的括号但是然后我在回报中最终得到引号,因为它是一个字符串,我不想要那些。那我该怎么办呢?
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
原因是我们希望开发人员调用我们的API能够使用动态脚本标记,如http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag所述。 HTML
4 个解决方案
#1
5
The technique you are after is called JSONP (JSON with Padding).
您所追求的技术称为JSONP(带填充的JSON)。
See How to support JSONP in WCF services:
请参见如何在WCF服务中支持JSONP:
The
JsonPEncoder
is a wrapping encoder on the WCF JSON encoder. It delegates most calls to the wrapped encoder. TheWriteMesage
methods have been overriden to pad the outgoing message with the callback method. TheJsonBehavior
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 theCallBack
property of the behavior.JsonPEncoder是WCF JSON编码器上的包装编码器。它将大多数调用委托给包装好的编码器。 WriteMesage方法已被覆盖,以使用回调方法填充传出消息。 JsonBehavior用于服务操作以为该操作启用JSONP编码。保存回调函数名称的URI的查询参数被指定为行为的CallBack属性。
#2
5
In WCF 4.0 added support for JSONP. You may use
在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/
#3
1
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.
我不知道他们使用的是什么版本的HTML,但在我使用过的所有版本中,脚本标记中的src属性必须是一个URL。他们在某种程度上使用了一个函数,而我看不到它的工作原理。
Have you actually seen a dynamic script tag work?
您是否真的看过动态脚本标签?
#4
1
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.
我知道这是一个旧线程,但似乎每个人都坚持要改变WCF以支持JSONP,而这显然不是海报所要求的。我发布了这里和forums.asp.net,没有人花时间回复。
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.
我最终将它作为流返回。这会导致使用“原始”模式,WCF不会触及响应。链接:http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx。
#1
5
The technique you are after is called JSONP (JSON with Padding).
您所追求的技术称为JSONP(带填充的JSON)。
See How to support JSONP in WCF services:
请参见如何在WCF服务中支持JSONP:
The
JsonPEncoder
is a wrapping encoder on the WCF JSON encoder. It delegates most calls to the wrapped encoder. TheWriteMesage
methods have been overriden to pad the outgoing message with the callback method. TheJsonBehavior
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 theCallBack
property of the behavior.JsonPEncoder是WCF JSON编码器上的包装编码器。它将大多数调用委托给包装好的编码器。 WriteMesage方法已被覆盖,以使用回调方法填充传出消息。 JsonBehavior用于服务操作以为该操作启用JSONP编码。保存回调函数名称的URI的查询参数被指定为行为的CallBack属性。
#2
5
In WCF 4.0 added support for JSONP. You may use
在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/
#3
1
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.
我不知道他们使用的是什么版本的HTML,但在我使用过的所有版本中,脚本标记中的src属性必须是一个URL。他们在某种程度上使用了一个函数,而我看不到它的工作原理。
Have you actually seen a dynamic script tag work?
您是否真的看过动态脚本标签?
#4
1
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.
我知道这是一个旧线程,但似乎每个人都坚持要改变WCF以支持JSONP,而这显然不是海报所要求的。我发布了这里和forums.asp.net,没有人花时间回复。
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.
我最终将它作为流返回。这会导致使用“原始”模式,WCF不会触及响应。链接:http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx。