javascript - 使用 php 从 JSON 数组填充 Web 数据

标签 javascript php jquery json

我已经使用 JSON 几天了,我真的认为这是一种利用它交换数据的非常酷的方式...我正在使用 jquery mobile 构建一个应用程序,我试图在其中填充 Json 数据,到目前为止我已经尝试过这种方法:

从名为 movie-details.json 的 json 文件中,我得到了以下内容:

{"movies":[{"id":"1","name":"Dabangg2","picUrl":"http:\/\/www.naz8.com\/images\/Dabangg2.jpg"},{"id":"2","name":"Talassh","picUrl":"http:\/\/www.naz8.com\/images\/talassh.jpg"},{"id":"3","name":"JAB TAK HAI JAAN","picUrl":"http:\/\/www.naz8.com\/images\/jthj.jpg"},{"id":"4","name":"Khiladi 786","picUrl":"http:\/\/www.naz8.com\/images\/khiladi786.jpg"}]}

我可以通过以下方式获取数据来动态创建详细的 ListView :

<script type="text/javascript">

 $.getJSON("movie-details.json", function(movies){
   //Start off with an empty list every time to get the latest from server
   $('#movieList').empty();

   //add the movie items as list
   $.each(movies, function(i, movie){
     $('#movieList').append(generateMovieLink(movie));
   });

   //refresh the list view to show the latest changes
   $('#movieList').listview('refresh');

 });

  //creates a movie link list item
 function generateMovieLink(movie){

  //debugger;
  return '<li><a href="javascript:void(0)'
        + '" onclick="goToMovieDetailPage(\''
        + movie.name 
        + '\',\''
        + movie.picUrl +'\')">' 
        + movie.name 
        + '</a></li>';
 }

 function goToMovieDetailPage(movieName, moviePicUrl){

  //create the page html template
  var moviePage = $("<div data-role='page' data-url=dummyUrl><div data-role='header' data-add-back-btn='true'><h1>"
                  + movieName + "</h1></div><div data-role='content'><img border='0' src='" 
                  + moviePicUrl + "' width=204 height=288></img></div><div data-role='footer' data-position='fixed'><h4>" 
                  + movieName + "</h4></div></div>");

  //append the new page to the page container
  moviePage.appendTo( $.mobile.pageContainer );

  //go to the newly created page
  $.mobile.changePage( moviePage );
 }  

</script>

如何从 php 文件填充数据,例如 movie-details.php:

<?php
include('connection.php');
$var = array();
$sql = "SELECT * FROM movies";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo '{"movies":'.json_encode($var).'}';
?>

我必须声明哪些变量才能从 php 文件中的对象获取 json 数据?

例如:

//只是一个想法...我很困惑..

var url="....";

$.getJSON(url,函数(...){

$.each(电影.电影, 函数(i,...)

非常感谢您的帮助...

最佳答案

您应该对整个 shebang 进行 json 编码。

echo json_encode(array("movies" => $var));

对于jquery方法,$.getJSON('<url>', ...)会起作用的。

关于javascript - 使用 php 从 JSON 数组填充 Web 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764810/

相关文章:

php - 从关键字创建高质量、独特的反向链接,以提高 SEO 和搜索引擎排名

php - 如何在 WordPress 中将 DIV 包裹在子菜单周围 - 仅在第一级?

php - 使用 PHP Zend 创建 Windows Azure 部署证书 - 例如无法下载 RDP 文件

javascript - 将任何 (.) 句点替换为转义反斜杠,以便我可以选择元素

javascript - 仅在满足条件时执行一次 if 语句

javascript - Ionic 2/3 类似于 WhatsApp 的信使功能

javascript - 如何合并两个json对象得到一个json对象?

jquery - 使用 ('change' jQuery 插件设置选择标签样式时,在 "nice-select"上获取选择标签 .val() 不起作用

Javascript 获取 li 值并对其进行编码

javascript - string.length 返回未定义?