【问题标题】:Best way to display data via JSON using jQuery使用 jQuery 通过 JSON 显示数据的最佳方式
【发布时间】:2008-11-29 05:45:41
【问题描述】:

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

我知道我会包含一个 .each 来运行它以显示所有内容。

【问题讨论】:

  • Coughlin/Ryan,编辑您的问题以反映下面的讨论。答案部分不是为讨论而设计的(而是为此目的使用 cmets 或使用其他详细信息更新您的问题)。

标签: jquery json


【解决方案1】:

类似这样的:

$.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");
          });
        });

将采用一个 json 对象,该对象由 PHP 数组返回,并带有 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。

【讨论】:

  • 出于性能原因,$(content).appendTo("#product_list") 应该在循环之外。追加到 DOM 很慢,应该只做一次。
  • 是的,应该是这样,如果是这样的话,第一行应该是 content +=。但是在某些浏览器中附加到 DOM 比其他浏览器更快:)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    完美!谢谢杰,下面是我的 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
    

    【讨论】:

    • 我明白了,我改了: $.each(data.posts, function(i,post){ 所以 data.post -> data.posts
    【解决方案4】:

    您可以从 JSON 对象创建 jQuery 对象:

    $.getJSON(url, data, function(json) {
        $(json).each(function() {
            /* YOUR CODE HERE */
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多