【问题标题】:$.getJSON with php file$.getJSON 与 php 文件
【发布时间】:2016-11-05 00:06:18
【问题描述】:

大家好 :) 我是编程界的新手,所以我真的需要你们的帮助。我试图从一些数据库表中获取数据,不幸的是我的 $.getJson() 函数出了点问题。如果我运行 php 文件,它就可以工作,并且可以使用 script.js 中的函数。我的 html 也可以,所以我想这是 $get.JSON 错误。我不懂那么多javascript,所以我把所有的希望都寄托在你身上:*

html 文件:

 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8" />
 <title>jQuery Ajax - PHP</title>
 <script type="text/javascript" src="jquery.js"></script>
 </head>
 <body>
 <script src="script.js"> 
 </script>
 </body>
 </html>

script.js 文件:

$('document').ready( function() {
                                done();
                                }
                    );
function done() {
    setTimeout(function() { 
                         updates();
                         done();
                           }
              , 200 );
                 }
function updates(){ 
   $.getJSON( "read.php", function (data){

      $.each(data.array, function () {
          $("body").append("<li>Titlu: "+this['title']+"</li>
                            <li>Descriere: "+this['describtion']+"</li>");
                                     }
                           );

      $.each(data.array1, function () {
          $("body").append("<li>Id: "+this['id']+"</li>
                          <li>Category_Id: "+this['category_id']+"</li>
                          <li>Titlu: "+this['title']+"</li>
                          <li>Descriere: "+this['describtion']+"</li>");
                                        }
             );

    $.each(data.array2, function () {
        $("body").append("<li>Id: "+this['id']+"</li>
                          <li>Titlu: "+this['title']+"</li>
                          <li>Latitudine: "+this['location_latitude']+"</li>
                         <li>Longitudine:"+this['location_longitude']+"</li>
                         <li>Numar de telefon: "+this['phone_number']+"</li>
                          <li>Descriere: "+this['describtion']+"</li>");
                                     }
          );

    $.each(data.array3, function () {
        $("body").append("<li>Id: "+this['id']+"</li>
                    <li>Interest_point_id:"+this['interest_point_id']+"</li>
                    <li>Pret: "+this['price']+"</li>
                    <li>Data: "+this['event1_time']+"</li>");
                                      }
          );
                                          }
                );

              }

最后是 read.php 文件(这里它向我展示了我的期望,所以我认为一切都很好):

<?php
include_once ('db.php');
$query= "SELECT * FROM category";
$query1= "SELECT * FROM sub_category";
$query2= "SELECT * FROM interest_point";
$query3= "SELECT * FROM event1";
global $connect;
$result =  mysqli_query($connect,$query);
$result1 = mysqli_query($connect,$query1);
$result2 = mysqli_query($connect,$query2);
$result3 = mysqli_query($connect,$query3);
$array = array();
$array1 = array();
$array2 = array();
$array3 = array();

while($row=mysqli_fetch_array($result))
    array_push($array , array( 'id'          => $row[0],
                               'title'       => $row[1],
                               'describtion' => $row[2]
));

while($row1=mysqli_fetch_array($result1))
    array_push($array1 , array( 'id'         => $row1[0],
                               'category_id' => $row1[1],
                               'title'       => $row1[2],
                               'describtion' => $row1[3]

));
while($row2=mysqli_fetch_array($result2))
    array_push($array2 , array('id'                => $row2[0],
                               'title'             => $row2[1],
                               'location_latitude' => $row2[2],
                               'location_longitude'=> $row2[3],
                               'phone_number'      => $row2[4],
                               'describtion'       => $row2[5]

));
while($row3=mysqli_fetch_array($result3))
    array_push($array3 , array( 
                               'id'               => $row3[0],
                               'interest_point_id'=> $row3[1],
                               'price'            => $row3[2],
                               'event1_time'      => $row3[3]
));

    echo json_encode(array("array"=>$array)).'<br>'.'<br>';
    echo json_encode(array("array1"=>$array1)).'<br>'.'<br>';
    echo json_encode(array("array2"=>$array2)).'<br>'.'<br>';
    echo json_encode(array("array3"=>$array3)).'<br>'.'<br>';


?>

【问题讨论】:

  • “出了点问题”不是一个好的解释。出了什么问题?
  • 请包含错误消息或日志中有关失败的$.getJSON 调用的任何信息。

标签: javascript php jquery html getjson


【解决方案1】:

发送json时只能回显一次,并且只能有一个php数组包含所有数据。您不能打印除此之外的任何内容,例如 &lt;br&gt; 标签

尝试改变

echo json_encode(array("array"=>$array)).'<br>'.'<br>';
echo json_encode(array("array1"=>$array1)).'<br>'.'<br>';
echo json_encode(array("array2"=>$array2)).'<br>'.'<br>';
echo json_encode(array("array3"=>$array3)).'<br>'.'<br>';

$output = array(
   "array1"=>$array1,
   "array2"=>$array2,
   "array3"=>$array3
);

echo json_encode($output);

还要注意&lt;li&gt;&lt;body&gt; 的无效子代。使用&lt;div&gt; 或插入&lt;ul&gt;

【讨论】:

  • 非常感谢你 :) 现在可以使用了 :D :D :D 上帝保佑你! :*
猜你喜欢
  • 1970-01-01
  • 2012-07-17
  • 2012-07-04
  • 1970-01-01
  • 2012-05-03
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多