首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用jQuery通过JSON显示数据的最佳方式

使用jQuery通过JSON显示数据的最佳方式
EN

Stack Overflow用户
提问于 2008-11-29 05:45:42
回答 4查看 105.9K关注 0票数 17

我正在尝试通过使用jQuery的Ajax调用在我的页面上显示结果的最佳方法,您认为最好的方法是将其作为JSON或纯文本传递吗?我以前使用过ajax调用,但不确定哪个更好,对于JSON版本,从PHP页面生成的JSON文件中读取以显示结果的最佳方式是什么。

我知道我会包含一个.each来遍历它来显示它们。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2008-11-29 15:53:37

如下所示:

$.getJSON("http://mywebsite.com/json/get.php?cid=15",
        function(data){
          $.each(data.products, function(i,product){
            content = '<p>' + product.product_title + '</p>';
            content += '<p>' + product.product_short_description + '</p>';
            content += '<img src="' + product.product_thumbnail_src + '"/>';
            content += '<br/>';
            $(content).appendTo("#product_list");
          });
        });

将接受一个由PHP数组生成的json对象,该数组与products的键一起返回。例如:

Array('products' => Array(0 => Array('product_title' => 'Product 1',
                                     'product_short_description' => 'Product 1 is a useful product',
                                     'product_thumbnail_src' => '/images/15/1.jpg'
                                    )
                          1 => Array('product_title' => 'Product 2',
                                     'product_short_description' => 'Product 2 is a not so useful product',
                                     'product_thumbnail_src' => '/images/15/2.jpg'
                                    )
                         )
     )

要重新加载列表,只需执行以下操作:

$("#product_list").empty();

然后使用新参数再次调用getJSON。

票数 27
EN

Stack Overflow用户

发布于 2008-11-29 06:45:06

JQuery具有用于Ajax的内置json数据类型,并将数据转换为对象。PHP%还有内置的json_encode函数,可以将数组转换为json格式的字符串。节省了大量的解析和解码工作。

票数 11
EN

Stack Overflow用户

发布于 2008-11-29 16:08:08

太棒了!谢谢你,Jay,下面是我的HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Facebook like ajax post - jQuery - ryancoughlin.com</title>
<link rel="stylesheet" href="../css/screen.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
<!--[if IE]><link rel="stylesheet" href="../css/ie.css" type="text/css" media="screen, projection"><![endif]-->
<link href="../css/highlight.css" rel="stylesheet" type="text/css" media="screen" />
<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function(){
    $.getJSON("readJSON.php",function(data){
        $.each(data.post, function(i,post){
            content += '<p>' + post.post_author + '</p>';
            content += '<p>' + post.post_content + '</p>';
            content += '<p' + post.date + '</p>';
            content += '<br/>';
            $(content).appendTo("#posts");
        });
    });   
});
/* ]]> */
</script>
</head>
<body>
        <div class="container">
                <div class="span-24">
                       <h2>Check out the following posts:</h2>
                        <div id="posts">
                        </di>
                </div>
        </div>
</body>
</html>

和我的JSON输出:

{ posts: [{"id":"1","date_added":"0001-02-22 00:00:00","post_content":"This is a post","author":"Ryan Coughlin"}]}

当我运行我的代码时,我得到了这个错误:

object is undefined
http://localhost:8888/rks/post/js/jquery.js
Line 19
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/327231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档