您可以像这样使用JSONP执行此操作:
function insertReply(content) { document.getElementById('output').innerHTML = content;}// create script elementvar script = document.createElement('script');// assing src with callback namescript.src = 'http://url.to.json?callback=insertReply';// insert script to document and load contentdocument.body.appendChild(script);但是消息源必须知道,您希望它调用作为回调参数传递给它的函数。
使用谷歌API,它看起来像这样:
function insertReply(content) { document.getElementById('output').innerHTML = content;}// create script elementvar script = document.createElement('script');// assing src with callback namescript.src = 'https://www.googleapis.com/freebase/v1/text/en/bob_dylan?callback=insertReply';// insert script to document and load contentdocument.body.appendChild(script);检查当您将回调传递给google
api时数据的外观:https
:
//www.googleapis.com/freebase/v1/text/en/bob_dylan?
callback
=insertReply
这是有关JSONP的很好的解释:http :
//en.wikipedia.org/wiki/JSONP



