1

今天我需要一些关于我的一项工作的帮助。我试图在 JQuery 中显示我在 PHP 中从我的数据库中检索到的一些数据。

由于某些原因,我需要在 JQuery 中显示数据...

我的 PHP 和我的 JQuery 在同一个页面index.php上,但我的问题是我的 ajax 不会显示我的数据并且没有出现错误,所以我不知道我的错误到底在哪里。

这是我的代码:

<?php
    require 'inc/db-connect.php';

    $title = array();

    try{
        $sql   = "SELECT * FROM events";
        $req   = $db->query($sql);  
    }catch(PDOException $e){
        echo 'Erreur: '.$e->getMessage();
    }

    while($res = $req->fetch()){
        array_push($title, $res['title']);
    }

    echo json_encode($title);

    $req->closeCursor();
?>

<!DOCTYPE html>

<html lang="fr">
    <?php include('inc/head.html'); ?>

    <body>
        <div id="stage">

            /** 
            * ALL MY HTML GOES HERE 
            **/

        </div>
        <!-- END STAGE -->

        <script type="text/javascript">
            $(document).ready(function(){
                $.ajax({
                    data: "",
                    dataType: 'json',
                    success: function(data){
                        console.log(data);
                    },
                    error: function(){
                        console.log('Failed to load data');   
                    }
                });
            });
        </script>
    </body>
</html>

所以当我加载我的页面时,我的 JQuery 会执行“console.log('无法加载数据')”,这意味着有一个错误,但我找不到它。

有人可以帮助我吗?

Thnx,安托

编辑1:

我的 PHP 请求似乎没有问题,因为 echo 显示了我从数据库中检索到的数据。显然错误来自我的 ajax 请求。

我已经在“index.php”上设置了 URL 参数,但也不起作用。

编辑2:

经过一些研究,似乎不可能在同一页面上发出 PHP 请求和 ajax 请求。所以我会尝试别的...

4

2 回答 2

1

你目前正在做的是这样的:

您正在创建一个如下所示的输出:

{"your json is": "here"}
<html>
  ... rest of html
  <script> doing an ajax request to get the json </script>
  ...
</html>

现在有多个问题。

  • html输出前有json
  • 有一个ajax调用试图获取你刚刚输出的文件,但是json后面有html,所以它不理解json

要解决此问题,您可以将其放入 2 个不同的文件中。

获取我的json.php

<?php
    require 'inc/db-connect.php';

    $title = array();

    try{
        $sql   = "SELECT * FROM events";
        $req   = $db->query($sql);  
    }catch(PDOException $e){
        echo 'Erreur: '.$e->getMessage();
    }

    while($res = $req->fetch()){
        array_push($title, $res['title']);
    }

    header('Content-type: application/json');
    echo json_encode($title);

    $req->closeCursor();
    exit();

索引.php

<html lang="fr">
    <?php include('inc/head.html'); ?>

    <body>
        <div id="stage">

            /** 
            * ALL MY HTML GOES HERE 
            **/

        </div>
        <!-- END STAGE -->

        <script type="text/javascript">
            $(document).ready(function(){
                $.ajax({
                    url: "getmyjson.php"
                    data: "",
                    dataType: 'json',
                    success: function(data){
                        console.log(data);
                    },
                    error: function(){
                        console.log('Failed to load data');   
                    }
                });
            });
        </script>
    </body>
</html>

或者您可以将其放在一个文件中并使用条件将其拆分

<?
if(isset($_GET['getmyjson']))
{
  // output json (incl header)
  exit();
}
else // why an else when you already did an exit.. just to be safe when you or someone else is modifying the code
{
  // output your main page with the ajax script that calls "url: '?getmyjson=true'"
}
于 2013-04-29T13:23:01.607 回答
0

Set the header of the content to type of json... Here is an example of setting header type.

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

Any malformed JSON is rejected and a parse error is thrown.

于 2013-04-29T12:49:28.707 回答