简体   繁体   中英

Read JSON from URL

I'm facing a problem which should be really simple to solve, but I am lost as of now.

I have a url: http://search.twitter.com/search.json?q=bacon

Using JavaScript (not JQuery or php. Just JavaScript), I want to read this JSON string and parse it. That's it.

Thanks!

You'll be restricted by the SOP ( XMLHttpRequest s can only be made to URI's on the same domain; JSON can only be retrieved via this method). To bypass this you'll have to use JSONP instead ( other explanation* ).

It seems the endpoint supports JSONP , so you can do:

function foo(response) {
    // response is already a JavaScript object
}

var script = document.createElement("script");
script.src = "http://search.twitter.com/search.json?q=bacon&callback=foo";

document.body.appendChild(script);

* Disclaimer: Yep, thats my blog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM