Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery通过JSON显示数据的最佳方式_Jquery_Json - Fatal编程技术网

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

使用jQuery通过JSON显示数据的最佳方式,jquery,json,Jquery,Json,我正在试图找到使用jQuery通过Ajax调用在我的页面上显示结果的最佳方法,您认为最好的方法是将其作为JSON或纯文本传递吗?我以前使用过ajax调用,但不确定哪种调用优于另一种调用,对于JSON版本,读取PHP页面生成的JSON文件以显示结果的最佳方式是什么 我知道我会包含一个。每个都会在其中运行以显示它们。JQuery有一个内置的用于Ajax的json数据类型,并将数据转换为对象。PHP%还有内置的json_encode函数,可将数组转换为json格式的字符串。节省了大量解析和解码工作。您

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


我知道我会包含一个
。每个
都会在其中运行以显示它们。

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

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

$.getJSON(url, data, function(json) {
    $(json).each(function() {
        /* YOUR CODE HERE */
    });
});
大概是这样的:

$.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");
          });
        });
要重新加载列表,只需执行以下操作:

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

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

完美!谢谢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>
当我运行代码时,出现以下错误:

object is undefined
http://localhost:8888/rks/post/js/jquery.js
Line 19

我明白了,我改变了:$。每个(data.posts,function(I,post){So data.post->data.postshoughlin/Ryan,编辑您的问题以反映下面的讨论。答案部分不是为讨论而设计的(而是为此目的使用注释或用其他细节更新您的问题)。出于性能原因,$(content)。appendTo(“#product_list”)应该在循环之外。附加到DOM的速度很慢,只能执行一次。是的,应该这样做,第一行应该是content+=如果是这样的话。但是在某些浏览器中附加到DOM的速度比其他浏览器快:)
{ 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