相当于以下 PHP 代码的 JQuery 是什么? (JSON)
我在 PHP 中得到了以下代码:
<?php
$json = file_get_contents('https://graph.facebook.com/192655950766049');
$data = json_decode($json, true);
echo $data['description'];
?>
我想要在 JQuery 中得到等效的代码。 我尝试自己做,但没有成功。 这是我的众多尝试之一:
<script type="text/javascript" src="../scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$.getJSON("https://graph.facebook.com/192655950766049", function(json) {
alert("JSON Data: " + json.description);
});
</script>
我阅读了 http://api.jquery.com/jQuery 的解释。 getJSON/,但我还不太明白..
无论如何,如果你能帮助我,那就太好了!
谢谢
I got the following code in PHP:
<?php
$json = file_get_contents('https://graph.facebook.com/192655950766049');
$data = json_decode($json, true);
echo $data['description'];
?>
and I want the equivalent code in JQuery.
I tried to do it myself but I had no luck.
Here's one of my many tries:
<script type="text/javascript" src="../scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$.getJSON("https://graph.facebook.com/192655950766049", function(json) {
alert("JSON Data: " + json.description);
});
</script>
I read the explanation from http://api.jquery.com/jQuery.getJSON/, but yet I dont really understand it..
anyway, if you can help me it'll be very nice!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您使用 Javascript 请求不同的域,违反了同源政策。您需要在服务器端执行此操作(即在您的情况下使用 PHP)。
You're violating the Same origin policy by requesting a different domain with Javascript. You'll need to do this server side (i.e. with PHP in your case).
您需要使用jquery的jsonp请求。请参阅此简短讨论,了解如何使用此功能与 Facebook 进行交互。 JSONP 允许 XSS 忽略同源策略。
基本上你的代码看起来类似于:
You need to use jquery's jsonp request. See this short discussion on how to interface with facebook using this. JSONP allows for XSS ignoring the same origin policy.
Basically your code would then look similar to this:
由于跨站脚本编写 限制。
You won't be able to directly download data from facebook on the client side due to cross site scripting limitations.
您也可以检查此网址。
http ://www.prettyklicks.com/blog/making-a-facebook-feed-using-the-graph-api-json-and-jquery/291/
另请使用与我相同的方法检查此链接指定的。
在循环中使用 $.getJSON
You can also check this url.
http://www.prettyklicks.com/blog/making-a-facebook-feed-using-the-graph-api-json-and-jquery/291/
Also check this link using the same method as i have specified.
using $.getJSON within a loop
如果有人有兴趣在本地使用 jquery IE:WAMP,这里是一个经过测试的示例。它正在读取的 z.txt 文件中有一个简单的单个数字。
In the event anyone is interested in using jquery locally IE: WAMP, here is a tested example.. The z.txt file it is reading has a simple single number in it..