I am trying to get json data from twitter . http://api.twitter.com/1/statuses/public_timeline.json
this is my code
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://api.twitter.com/1/statuses/public_timeline.json",function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
it is from http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_getjson
but it is not working !
You are running in the same origin policy in JavaScript. This basically says, that you can’t access resources from different domains (in your case the other domain would be twitter.com).
One solution is to use JSONP. The twitter API supports this. You can run a JSONP-request with jQuery in your case like this:
Besides, w3schools is no reliable source for information. Better use something like the Mozilla Developer Network.