使用Jquery将数据提取为JSON [英] Pull data our of JSON with Jquery

查看:63
本文介绍了使用Jquery将数据提取为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json文件存储在静态网址中,我想抓取它并提取我们的数据对象.

I have a json file being stored in a static url, i would like to grab it and pull our the data objects.

<div id="content"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $.getJSON('https://s3.amazonaws.com/wallyball_production/comedy.json', function(data){
            $("#content").html(data);
        });

    });
  </script>

这什么都不输出?我做得很快,不确定为什么我什么都没看见吗?

This isn't outputting anything? I did it very quickly, not sure why I'm not seeing anything?

推荐答案

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

Cross-domain AJAX calls require jsonp (or writing a proxy server-side script). As long as the remote-server is setup properly (I'd think Amazon would be) it's pretty easy with 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.

Note that you will get JSON in response so you will need to iterate through it before appending it to the DOM.

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

Here are docs for jQuery's $.ajax(): http://api.jquery.com/jquery.ajax

这篇关于使用Jquery将数据提取为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆