gpt4 book ai didi

.net - 从 WCF REST Web 服务返回包装在回调函数中的 JSON

转载 作者:行者123 更新时间:2023-12-04 17:05:50 25 4
gpt4 key购买 nike

我有一个返回 JSON 的 Web 服务,但现在我想修改它以允许调用者指定回调函数,以便返回从:JSON DATA 到 specifiedFunction(JSON DATA);我现在返回 JSON 的方式只是返回一个对象的实例并让 .NET 执行其序列化魔术,如果我更改为仅返回一个字符串,我可以添加函数的名称和数据周围的括号,但是然后我最终在返回中使用引号,因为它是一个字符串,我不想要那些。那我该怎么办呢?

这样做的原因是我们希望调用我们的 API 的开发人员能够使用动态脚本标签,如这里 http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html 所述。

最佳答案

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

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.

关于.net - 从 WCF REST Web 服务返回包装在回调函数中的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1178614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com