我的网站上有一个javascript代码,有一个变量:
var remoteJsonVar;
Run Code Online (Sandbox Code Playgroud)
另一方面,远程网站上有一个json文件
https://graph.facebook.com/?ids=http://www.stackoverflow.com
我需要将变量remoteJsonVar设置为这个远程jason数据.
我确信这很简单,但我找不到解决方案.
一个小的工作示例会很好.
因为您试图从不同的来源获取数据,如果您想完全在客户端执行此操作,则由于相同原始策略,您将使用JSON-P而不仅仅是JSON .如果你只是在你的查询字符串中添加一个参数,Facebook支持这个,例如:callback
https://graph.facebook.com/?ids=http://www.stackoverflow.com?callback=foo
Run Code Online (Sandbox Code Playgroud)
然后在脚本中定义一个函数(在全局范围内),该函数具有您在该callback参数中给出的名称,如下所示:
function foo(data) {
remoteJsonVar = data;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过创建script元素并将其设置src为所需的URL来触发它,例如:
var script = document.createElement('script');
script.src = "https://graph.facebook.com/?ids=http://www.stackoverflow.com?callback=foo";
document.documentElement.appendChild(script);
Run Code Online (Sandbox Code Playgroud)
请注意,对函数的调用将是异步的.
现在,既然你可能想要有多个未完成的请求,并且你可能不希望在完成后留下那个回调,你可能想要更复杂并创建一个随机回调名称等等这是一个完整的例子:
(function() {
// Your variable; if you prefer, it could be a global,
// but I try to avoid globals where I can
var responseJsonVar;
// Hook up the button
hookEvent(document.getElementById("theButton"),
"click",
function() {
var callbackName, script;
// Get a random name for our callback
callbackName = "foo" + new Date().getTime() + Math.floor(Math.random() * 10000);
// Create it
window[callbackName] = function(data) {
responseJsonVar = data;
display("Got the data, <code>shares = " +
data["http://www.stackoverflow.com"].shares +
"</code>");
// Remove our callback (`delete` with `window` properties
// fails on some versions of IE, so we fall back to setting
// the property to `undefined` if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
}
// Do the JSONP request
script = document.createElement('script');
script.src = "https://graph.facebook.com/?ids=http://www.stackoverflow.com&callback=" + callbackName;
document.documentElement.appendChild(script);
display("Request started");
});
// === Basic utility functions
function display(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
}
function hookEvent(element, eventName, handler) {
// Very quick-and-dirty, recommend using a proper library,
// this is just for the purposes of the example.
if (typeof element.addEventListener !== "undefined") {
element.addEventListener(eventName, handler, false);
}
else if (typeof element.attachEvent !== "undefined") {
element.attachEvent("on" + eventName, function(event) {
return handler(event || window.event);
});
}
else {
throw "Browser not supported.";
}
}
})();
Run Code Online (Sandbox Code Playgroud)
请注意,当您使用JSONP时,您会在另一端对该站点充满信任.从技术上讲,JSONP根本不是JSON,它使远程站点有机会在您的页面上运行代码.如果你相信另一端,那很好,但要记住滥用的可能性.
你没有提到使用任何库,所以我没有使用任何上面的,但我建议你看一个很好的JavaScript库,如jQuery,Prototype,YUI,Closure或其他几个.上面的很多代码已经为你编写了一个很好的库.例如,以上是使用jQuery:
jQuery(function($) {
// Your variable
var responseJsonVar;
$("#theButton").click(function() {
display("Sending request");
$.get("https://graph.facebook.com/?ids=http://www.stackoverflow.com&callback=?",
function(data) {
responseJsonVar = data;
display("Got the data, <code>shares = " +
data["http://www.stackoverflow.com"].shares +
"</code>");
},
"jsonp");
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2634 次 |
| 最近记录: |