【问题标题】:Extracting information from database using JSON and AJAX使用 JSON 和 AJAX 从数据库中提取信息
【发布时间】:2018-11-08 02:01:57
【问题描述】:

我正在尝试使用 Javascript、AJAX、php 和 JSON 制作一个简单的“搜索”框。对于这个项目,我只是使用从 mqsql 网站下载的数据库“world”。 我在尝试从我的数据库中提取信息时遇到了问题。它只需要第一行信息,然后我就得到了 错误:“SCRIPT5007:SCRIPT5007:无法获取未定义或空引用的属性“大陆” ajaj.js (65,3)"

提前感谢您能给我的任何帮助!

这是我的 ajaj.js 代码:

var ajaxRequest=new XMLHttpRequest();
var input;
var button;

function init(){
    input = document.getElementById('search');
    input.addEventListener("keyup", sendRequest, false);
    button = document.getElementById('sendButton');
    button.addEventListener("click", sendSecondRequest, false);
}


function sendRequest(){
    ajaxRequest.onreadystatechange = getRequest;

    var searchTxt = "country=" + input.value;

    ajaxRequest.open("GET", "getCountries.php?" + searchTxt, true);
    ajaxRequest.send();
}


function getRequest(){
    if (ajaxRequest.readyState==4 && ajaxRequest.status==200){
        var jsonObj = JSON.parse(ajaxRequest.responseText);

        var dataList = document.getElementById('countries');

        dataList.innerHTML="";

        for(var i = 0; i<jsonObj.length; i++){

            var option = document.createElement('option');

            option.value = jsonObj[i]['Name'];

            dataList.appendChild(option);
        }
    }
}

function sendSecondRequest(){
    ajaxRequest.onreadystatechange = getSecondRequest;

    var infoCountry = "country=" + input.value;

    ajaxRequest.open("GET", "getCountries2.php?" + infoCountry, true);
    ajaxRequest.send();
}

function getSecondRequest(){
    if (ajaxRequest.readyState==4 && ajaxRequest.status==200){
        alert(ajaxRequest.responseText);
        var jsonObj2 = JSON.parse(ajaxRequest.responseText);




        document.getElementById("result").innerHTML = "LANDSKOD: " + jsonObj2[0]["Code"] + "<br>";
        document.getElementById("result2").innerHTML = "LANDSKOD: " + jsonObj2[2]["Continent"] + "<br>";
        document.getElementById("result3").innerHTML = "LANDSKOD: " + jsonObj2[7]["Population"] + "<br>";

    }
}

window.addEventListener("load",init,false);

问题就出在这里,我只是不知道如何让它工作:

document.getElementById("result").innerHTML = "LANDSKOD: " + jsonObj2[0]["Code"] + "<br>";
document.getElementById("result2").innerHTML = "LANDSKOD: " + jsonObj2[2]["Continent"] + "<br>";
document.getElementById("result3").innerHTML = "LANDSKOD: " + jsonObj2[7]["Population"] + "<br>";

我尝试了几种不同的解决方案,但无法正常工作,我尝试将代码合并到一行中:

document.getElementById("result").innerHTML = "LANDSKOD: " + jsonObj2[0]["Code"] + "<br>" + "Continent: " + jsonObj2[2]["Continent] + "<br>"; 

但这似乎对我也不起作用。

我的其余代码:

index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>AJAX</title>
    <script type="text/javascript" src="ajaj.js"></script>
</head>
<body>
<h1>Sök information om ett land</h1>

<input type="text" list="countries"  id = 'search' placeholder="Land">
<datalist id="countries">
</datalist>
<button type="button" id="sendButton">Hämta information</button>

<p id="result"></p>
<p id="result2"></p>
<p id="result3"></p>

</body>
</html>

getCountries.php

<?php
include_once('db.inc.php');

$country = $_GET['country'];

// Kör frågan mot databasen world och tabellen country
$stmt = $db->prepare("SELECT * FROM country WHERE Name Like ? ORDER BY Name");
$stmt->execute(array("$country%"));

$tableRows = array();

// Lägger resultatet i vår array
$tableRows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Här konverteras och skickas resultatet i JSON-format
echo json_encode($tableRows);
?>

getCountries2.php

<?php
include_once('db.inc.php');

$country = $_GET['country'];

// Kör frågan mot databasen world och tabellen country
$stmt = $db->prepare("SELECT * FROM country WHERE Name Like ? ORDER BY Name");
$stmt->execute(array("$country"));

$tableRows = array();

// Lägger resultatet i vår array
$tableRows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Här konverteras och skickas resultatet i JSON-format
echo json_encode($tableRows);
?>

db.inc.php

<?php

define ('DB_USER', 'root');
define ('DB_PASSWORD', 'Abc12345');
define ('DB_HOST', 'localhost');
define ('DB_NAME', 'world');

$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
?>

编辑:我刚从 cmets 得到答案,信息在同一行,没有不同,因此它不适用于

jsonObj2[2]["Continent"]

但它确实适用于

jsonObj2[0]["Continent"]

谢谢@sean!

【问题讨论】:

  • 您确定您的 Continent 列在您的数据库中命名为 Continent(使用 C 而不是 c)?而不是SELECT * FROM ... 尝试一一获取您的数据也许可以确定。
  • @MickaelLeger 是的,我确定,这是我从服务器/数据库 gyazo.com/3be9699ea6fc2477c174803ac86a6a59 得到的 JSON 响应
  • 您确定要第三行值 - jsonObj2[2]... 而不是第一行值 - jsonObj2[0]["Continent"]?我认为您混淆了行与列。
  • 我认为 Sean 是对的,你得到的 JSON 响应包含一个具有不同键的 Obj,所以你想找到 jsonObj2[0]["Continent], same for jsonObj2[7]["Population" ]` 我认为 -> jsonObj2[0]["Population"]
  • @Sean 你是对的,我实际上认为每条信息都在它的行上,它实际上只是在同一行上,使用 jsonObj2[0]["continent"] 效果很好!谢谢。

标签: javascript php mysql json ajax


【解决方案1】:

表示 jsonObj2[2] 不是对象。似乎您在数据库返回数组中没有 3 行(jsonObj2[2])。 试试

jsonObj2[0]["Code"] + jsonObj2[0]["Continent"] + sonObj2[0] 
["Population"]

它应该可以工作,但不知道为什么要定位到第 0、2、7 行。

【讨论】:

  • 我只是对行与列感到困惑,我认为每条信息都在自己的行上。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2013-08-18
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 2017-12-23
相关资源
最近更新 更多