How to load external JSON from a script on a web page?

I am trying to load data from an external .js file containing a json representation of a data set. I can’t understand for life how to access the data inside the page. I am sure it is very simple, and I missed something simple! right now, I'm trying this:

  $(document).ready(function(){ $.getJSON("http://api.crunchbase.com/v/1/company/xobni.js", function(data){ alert(data.company_url); }); }); 

... which is clearly wrong, because nothing is happening. I tried loading it into the <script> , but firebug tells me that it didn’t even load. how could i mess it up Anyway, I am ready to pull my hair out, and I believe that it will take about 15 seconds to understand.

+6
json javascript jquery
source share
3 answers

This data file has no company_url . Also, the .js file is filed with the text/javascript mime-type type when it should be sent with application/json (or application/x-javascript , correct me on this).

The real reason , of course, is that you need to add ?callback=? to your url. Then everything will work. So it will look like this:

 $(document).ready(function(){ $.getJSON("http://api.crunchbase.com/v/1/company/xobni.js?callback=?", function(data){ alert(data.homepage_url); }); }); 
+13
source share

I looked at the json data. There seems to be no company_url. You may need homepage_url

 $(document).ready(function(){ $.getJSON("http://api.crunchbase.com/v/1/company/xobni.js", function(data){ alert(data.homepage_url); }); }); 
+3
source share

At first glance it looks good. Are you sure the answer is really JSON? Perhaps the wrong content? Is the source URL in the same domain as your page? (including protocol and port number)

edit:

I loaded your JSON and there is no "company_url" property.

+1
source share

All Articles