使用jQuery.getJSON()用于Url

时间:2013-02-07 15:30:59

标签: jquery json

我如何使用jQuery.getJSON()获取此URL的数据?

http://gbrds.gbif.org/registry/organisation/a3c228d0-3110-11db-abb8-b8a03c50a862.json?op=contacts

如果我在浏览器中查看结果,我会得到以下结果:

[{"position":"","lastName":"","phone":"+39 06 6118286","type":"technical","city":"","country":"","isPrimaryContact":true,"postalCode":"","address":"","email":"m.skofic@cgiar.org","description":"","province":"","firstName":"Milko Skofic","salutation":"","key":"48"},{"position":"","lastName":"","phone":"39-06-6118204","type":"administrative","city":"","country":"","isPrimaryContact":true,"postalCode":"","address":"IPGRI, Via Tre Denari, 472/a, 00057, Maccarese, Rome, Italy,","email":"eurisco@cgiar.org","description":"","province":"","firstName":"Ms. Sonia Dias","salutation":"","key":"49"}]

3 个答案:

答案 0 :(得分:1)

查看jQuery文档中的getJSON方法。

Sintaxe:

$.getJSON(url, data, function success);

所以,你可以尝试这样的事情:

$.getJSON("http://gbrds.gbif.org/registry/organisation/a3c228d0-3110-11db-abb8-b8a03c50a862.json?op=contacts", null, function(data) {


      // loop in your result if it is an array
      $.each(data, function(i, item) {

         // use data[i].property to access each property of your array.
         // for sample:

         var p = data[i].position;
         var l = data[i].lastName;

      });​

   });

答案 1 :(得分:0)

如果您要求来自其他架构/主机/端口组合的网址(例如https://gbrds.gbif.com:8080),则由于违反了same-origin policy

,您的浏览器会抛出安全例外

如果你能实现jquery也支持的jsonp,那么就可以解决这个问题。

答案 2 :(得分:0)

您需要使用JSONP,因为它不在同一个域中。

$.getJSON("http://gbrds.gbif.org/registry/organisation/a3c228d0-3110-11db-abb8-b8a03c50a862.json?op=contacts&callback=?", function(data) {
    console.log(data);
});