使用Jquery提取JSON数据

我有一个json文件存储在静态URL中,我想抓住它并拉动我们的数据对象。

$(document).ready(function(){ $.getJSON('https://s3.amazonaws.com/wallyball_production/comedy.json', function(data){ $("#content").html(data); }); });

这不输出任何东西? 我很快就做到了,不知道为什么我什么都没看到?

跨域AJAX调用需要jsonp (或编写代理服务器端脚本)。 只要远程服务器设置正确(我认为亚马逊会这样),jQuery就很容易了:

 $.ajax({ url : 'https://s3.amazonaws.com/wallyball_production/comedy.json', dataType : 'jsonp', success : function (data) { //$('#content').html(data); for (var i = 0, len = data.length; i < len; i++) { //`data[i].something` will access the `something` property an index of the JSON returned } } }); 

请注意,您将获得响应中的JSON,因此您需要在将其附加到DOM之前对其进行迭代。

以下是jQuery的$.ajax()文档: http : //api.jquery.com/jquery.ajax

由于同源策略 ,您的Web应用程序无法与其他域中的内容进行交互。

您需要通过您的网络服务器代理请求(然后联系亚马逊并返回结果),或使用JSONP 。