【问题标题】:Json Rpc callback functionJson Rpc回调函数
【发布时间】:2012-02-06 13:46:12
【问题描述】:

如何将 Json Rpc 数据传递给指定的回调函数,就像 Json 一样。 可以通过在url中指定回调参数来获取响应数据。

例如:

var url = "http://...sample/..?alt=new&callback=dispUser";
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script); 

那么结果会是这样的

显示用户({ “ID”: ”” });

但是在 Json Rpc 中我不能,有没有办法通过声明回调来获取 Json Rpc 的响应数据。如果没有,我将如何在客户端显示这些数据。因为我只能使用 Json Rpc 或 SOAP XML 来获得这些 api 服务,所以这就是文档中所说的。

【问题讨论】:

  • 你在想 jsonp 而不是 json-rpc 吗?

标签: javascript callback jsonp json-rpc


【解决方案1】:

您的示例采用 JSONP 样式。这是 JSON-RPC 风格的示例:

var mathService;

function init() {
    mathService = RPC.consume('http://foo.bar/mathematics.smd', mathReady);
}

function mathReady() {
    mathService.cuberoot(9, function(root) {
        $('#example_output').html(root);
    });
}

window.onload = init;

如果 JSON-RPC 服务不通过 SMD 描述自己,您可以改为这样写:

function init() {
    RPC.callMethod('http://foo.bar/mathematics.php', { 
        method: 'cuberoot', 
        params: [ 9 ]
    }, function(error, result) {
        $('#example_output').html(result);
    });
}

window.onload = init;

有很多库可以从 JavaScript 客户端(例如:浏览器)执行 JSON-RPC,每个库的调用约定可能略有不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2012-07-27
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多