Printing the JSON data?
I am learning how to use JavaScript so I wrote a simple script that prints out all public Facebook statuses for a particular keyword. For some reason, it is not doing what I expect it to do. A sample URL where the JSON would be found would be: http://graph.facebook.com/search?q=Beatles
Right now, it is not printing any results. Can anyone spot the error in this?
<input type="text" id="query" /><button>add</button><br />
<div id="results">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var url='http://graph.facebook.com/search?q=';
var query;
$('button').cli开发者_如何学Pythonck(function(){
query=$("#query").val();
$.getJSON(url+query,function(json){
$.each(json.data,function(i,feed){
if(feed.type=='status') {
$("#results").append('<p>'+feed.message+'</p>');
}
});
});
});
});
</script>
You cannot retrieve data from an external URL (same origin policy) . It is only possible with JSONP, which the Facebook API supports. You have to add callback=?
to your URL in order to use it:
var url='http://graph.facebook.com/search?callback=?&q=';
(This is also done in the Twitter example you linked to in your comment to the now deleted answer)
精彩评论