使用Jquery将数据提取为JSON

编程入门 行业动态 更新时间:2024-10-12 01:28:55
本文介绍了使用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="ajax.googleapis/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $.getJSON('s3.amazonaws/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 : 's3.amazonaws/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()的文档: api.jquery/jquery.ajax

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

更多推荐

使用Jquery将数据提取为JSON

本文发布于:2023-11-30 19:28:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Jquery   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!