Json Rpc回调函数

Json Rpc callback function

本文关键字:函数 回调 Rpc Json      更新时间:2023-09-26

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

例如:

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

那么结果将类似于这个

dispUser({"id":"});

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

您的示例是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,每个库在调用约定方面可能略有不同。