如何从网页上的脚本加载外部JSON?

时间:2022-01-19 07:56:18

I'm trying to load data from an external .js file, containing a json representation of a bunch of data. I cannot for the life of me figure out how to access the data inside the page. I'm sure this is really easy and I'm missing something simple! right now, I'm trying this:

我正在尝试从外部.js文件加载数据,其中包含一堆数据的json表示。我不能为我的生活弄清楚如何访问页面内的数据。我确信这很简单,我错过了一些简单的东西!现在,我正在尝试这个:

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

...which is obviously very wrong, since nothing happens. I've tried loading it in a <script> tag, but firebug tells me it didn't even load. how could I screw that up? anyway, I'm about ready to pull my hair out, and I figure this will take someone else about 15 seconds to figure out.

......这显然是非常错误的,因为没有任何反应。我已经尝试在

3 个解决方案

#1


13  

that data file doesn't have company_url entry. Additionally, the .js file is served with text/javascript mime-type, when it should be served with application/json (or application/x-javascript, correct me on that).

该数据文件没有company_url条目。另外,.js文件是使用text / javascript mime-type提供的,当应该使用application / json(或者application / x-javascript,请更正我)时。

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

当然,真正的原因是你需要添加?callback =?到你的网址。一切都会好起来的。所以,它看起来像这样:

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

#2


3  

I looked at the json data. It looks like there is no company_url. You might want homepage_url

我查看了json数据。看起来好像没有company_url。你可能想要homepage_url

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

#3


1  

Looks okay at first glance. Are you sure that the response is valid JSON? Is the content-type incorrect, perhaps? Is the source URL on the exact same domain as your page? (including protocol and port number)

乍一看似乎没问题。您确定响应是有效的JSON吗?可能是内容类型不正确吗?源网址与您的网页完全相同吗? (包括协议和端口号)

edit:

编辑:

I loaded your JSON, and there's no "company_url" property.

我加载了你的JSON,并且没有“company_url”属性。

#1


13  

that data file doesn't have company_url entry. Additionally, the .js file is served with text/javascript mime-type, when it should be served with application/json (or application/x-javascript, correct me on that).

该数据文件没有company_url条目。另外,.js文件是使用text / javascript mime-type提供的,当应该使用application / json(或者application / x-javascript,请更正我)时。

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

当然,真正的原因是你需要添加?callback =?到你的网址。一切都会好起来的。所以,它看起来像这样:

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

#2


3  

I looked at the json data. It looks like there is no company_url. You might want homepage_url

我查看了json数据。看起来好像没有company_url。你可能想要homepage_url

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

#3


1  

Looks okay at first glance. Are you sure that the response is valid JSON? Is the content-type incorrect, perhaps? Is the source URL on the exact same domain as your page? (including protocol and port number)

乍一看似乎没问题。您确定响应是有效的JSON吗?可能是内容类型不正确吗?源网址与您的网页完全相同吗? (包括协议和端口号)

edit:

编辑:

I loaded your JSON, and there's no "company_url" property.

我加载了你的JSON,并且没有“company_url”属性。