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

例如:

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,并且每个库的调用约定可能略有不同。

关于javascript - Json Rpc回调函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8798346/

10-10 23:12
查看更多