如何使用AJAX和JSON获取从PHP文件返回的数据

时间:2022-01-04 03:37:04

For starters this website is being run on a Debian machine. I have a SQLite3 database that has current news articles in it. I am trying to use PHP to query the database for these articles, and pass it as JSON to AJAX, so it can be displayed on my webpage. Right now nothing is being shown and I don't know where the error is.

对于初学者来说,这个网站是在Debian机器上运行的。我有一个SQLite3数据库,其中包含当前的新闻文章。我正在尝试使用PHP来查询这些文章的数据库,并将其作为JSON传递给AJAX,因此它可以显示在我的网页上。现在没有显示任何内容,我不知道错误在哪里。

Here is the PHP code to get the information from the database:

以下是从数据库获取信息的PHP代码:

<?php

class MyDB extends SQLite3
{
    function __construct()
    {
        $this->open('website.db');
    }
}

$db = new MyDB();
$result = $db->query('SELECT * FROM news');
echo json_encode($result);
?>

Here is the JavaScript where the AJAX is located:

这是AJAX所在的JavaScript:

<script type="text/javascript">

 function getNews()
 {
     console.log("firstStep");
      $(document).ready(function()
      {

        console.log("secondStep");
        $.getJSON("http://localhost/getNews.php",function(result){

            console.log("thirdStep");
            $('news').append(result); // display result

                 });
       });
  }

I think the error is occurring around $.getJSON("http://localhost/getNews.php",function(result), as in the console, thirdStep is never being outputted.

我认为错误发生在$ .getJSON(“http://localhost/getNews.php”,函数(结果))周围,就像在控制台中一样,从不输出thirdStep。

This is the HTML it should be appending to: <div id = "newsEntry"> <news> test </news> </div>

这是它应附加到的HTML:

test

Any help would be appreciated.

任何帮助,将不胜感激。

2 个解决方案

#1


1  

To find out what's going on, you might want to add an error handler:

要了解发生了什么,您可能需要添加错误处理程序:

$(document).ready(function() {
  $.ajax({
    url: "http://localhost/getNews.php",
    dataType: "json",
    success: function(result) {
      console.log("thirdStep");
    },
    error: function(err) {
      alert(err);
    }
  });
})

#2


1  

By default, the web server serves content as application/html. So when you simply echo a JSON string, it's treated like text on a html page. To really return JSON from your server, you need to specifically set it.

默认情况下,Web服务器将内容作为application / html提供。因此,当您只是回显一个JSON字符串时,它就像html页面上的文本一样。要真正从服务器返回JSON,您需要专门设置它。

Include this line before your echo:

在回声之前包括这一行:

header('Content-Type: application/json; charset=utf-8');

Edit

On inspection of you PHP code, you are missing one line. Note that $db->query() returns you an SQLite3Result. You need to call:

在检查你的PHP代码时,你缺少一行。请注意,$ db-> query()会返回一个SQLite3Result。你需要打电话:

$array = $result->fetchArray(SQLITE3_ASSOC);  // get an associative array first
$json = json_encode($array);
header('Content-Type: application/json; charset=utf-8');
echo $json

#1


1  

To find out what's going on, you might want to add an error handler:

要了解发生了什么,您可能需要添加错误处理程序:

$(document).ready(function() {
  $.ajax({
    url: "http://localhost/getNews.php",
    dataType: "json",
    success: function(result) {
      console.log("thirdStep");
    },
    error: function(err) {
      alert(err);
    }
  });
})

#2


1  

By default, the web server serves content as application/html. So when you simply echo a JSON string, it's treated like text on a html page. To really return JSON from your server, you need to specifically set it.

默认情况下,Web服务器将内容作为application / html提供。因此,当您只是回显一个JSON字符串时,它就像html页面上的文本一样。要真正从服务器返回JSON,您需要专门设置它。

Include this line before your echo:

在回声之前包括这一行:

header('Content-Type: application/json; charset=utf-8');

Edit

On inspection of you PHP code, you are missing one line. Note that $db->query() returns you an SQLite3Result. You need to call:

在检查你的PHP代码时,你缺少一行。请注意,$ db-> query()会返回一个SQLite3Result。你需要打电话:

$array = $result->fetchArray(SQLITE3_ASSOC);  // get an associative array first
$json = json_encode($array);
header('Content-Type: application/json; charset=utf-8');
echo $json