Read JSON from URL

I ran into a problem that should be really easy to solve, but I'm lost at the moment.

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. What is it.

Thanks!

+6
source share
1 answer

You will be limited to SOP ( XMLHttpRequest can only be done in a URI in the same domain; JSON can only be retrieved using this method). To get around this, you will have to use JSONP instead ( another explanation * ).

The endpoint seems to support 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: Yes, this is my blog

+14
source

Source: https://habr.com/ru/post/915694/


All Articles