Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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_Html_Json - Fatal编程技术网

尝试使用jquery获取json数据

尝试使用jquery获取json数据,jquery,html,json,Jquery,Html,Json,我正在从php文件输出json数据(我的输出示例) 如何使用jquery getjson或get and using each向div#news显示所有这些数据?这将使用jquery的append()方法向新闻div添加每篇文章的h2和p标记。显然,您可以根据需要自定义$。每个回调 更新未定义错误的原因是each()方法的回调缺少作为第一个参数的数组索引。我适当地更新了我的示例 <script type="text/javascript"> jQuery(function($){

我正在从php文件输出json数据(我的输出示例)


如何使用jquery getjson或get and using each向div#news显示所有这些数据?

这将使用jquery的
append()
方法向
新闻div添加每篇文章的
h2
p
标记。显然,您可以根据需要自定义
$。每个
回调

更新未定义错误的原因是
each()
方法的回调缺少作为第一个参数的数组索引。我适当地更新了我的示例

<script type="text/javascript">
  jQuery(function($){
    $.getJSON('uri/to/file.php', function(data) {
      $.each(data['news'], function(i, article){
        $('#news').append('<h2>' + article['title'] + '</h2><p>' + article['content'] + '</p>');
      });
    });
  });
</script>

jQuery(函数($){
$.getJSON('uri/to/file.php',函数(数据){
$.each(数据['news'],函数(i,文章){
$(“#新闻”)。追加(“+文章['title']+'”+文章['content']+'

”); }); }); });
我用这种方法解决了这个问题,通过在新的变量中加入title和content的内容,我不知道为什么第一种方法不起作用,但这种方法非常有效:)

查询(函数($){ $.getJSON('file.php',函数(数据){ $.each(数据['news'],函数(i,行){ var titulli=行标题; var contenti=row.content; $(“#新闻”)。追加(“+title+”“+content+”

); }); }); });
我得到了6行未定义的h2和6行未定义的p:/each()回调的第一个参数好像忘了。我相应地更新了示例:)
<script type="text/javascript">
  jQuery(function($){
    $.getJSON('uri/to/file.php', function(data) {
      $.each(data['news'], function(i, article){
        $('#news').append('<h2>' + article['title'] + '</h2><p>' + article['content'] + '</p>');
      });
    });
  });
</script>
Query(function($){
    $.getJSON('file.php', function(data) {
        $.each(data['news'], function(i,row){
        var titulli = row.title;
        var contenti = row.content;
    $('#news').append('<h2>' + title + '</h2><p>' + content + '</p>');


      });
    });
  });