Can't load json data in jquery Can't load json data in jquery json json

Can't load json data in jquery


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:

$.ajax({  url: "http://api.twitter.com/1/statuses/public_timeline.json",  cache: false,  dataType: 'jsonp',  success: function( result ){    $.each(result, function(i, field){                        $("div").append(field + " ");                    });  }});

Besides, w3schools is no reliable source for information. Better use something like the Mozilla Developer Network.


Check out the json document you are trying to load. It returns the following error when I try to access it:

{"errors":[{"message":"Sorry, that page does not exist","code":34}]}

I would try replacing the url with a working api call to twitter before you worry about whether the code snippet is working. :)


Your trying to get a document from a different domain and the requested URL is blocking your request due to Cross-Origin Resource Sharing. You can read up on that here: http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

You can use JSONP with jQuery which should allow you to complete the request but that page you are requesting gets a 404 error so there's probably not much you can do with the result anyway.