2012-01-10 40 views
1

如何將Json Rpc數據傳遞給指定的回調函數,就像Json一樣。 您可以通過在url中指定回調參數來獲取響應數據。Json Rpc回調函數

例如:

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服務,這就是文檔所告訴的。

+0

你在想_jsonp_而不是_json-rpc_嗎? – 2012-04-03 09:10:44

回答

2

你的例子是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的庫,並且在調用約定中每個庫都可能略有不同。

+0

更正了很多錯誤: - / – 2012-04-03 09:33:30