基本上我想做的事情:
到目前为止我尝试过:
string "Array"
我浪费了8个小时,我失去了希望。
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
</head>
<body>
<div id="output"></div>
//Some scripts...
</body>
</html>
PHP:
<!doctype html>
<html>
<head>
<title>Title</title>
</head>
<?PHP
//Got the connection, recieved data and now putting it to an array
$qry = "SELECT * FROM img LIMIT 0,9";
$qry_result = mysql_query($qry) or trigger_error(mysql_error(). " in ".$qry);
while($row = mysql_fetch_array($qry_result, MYSQL_BOTH)){
//Store values in multidimensional array for returning
$arraytoreturn[] = $row;
}
//close the connection and return array
mysql_close($dbhandle);
echo json_encode($arraytoreturn);
?>
<body>
</body>
</html>
JavaScript(只有与我的问题有关的函数):
function drawOutput(response) {
"use strict";
var array = JSON.parse(response);
var container = document.getElementById('output');
//Try out the code with first item and "ID" that is for sure in my array
container.innerHTML = array[0].ID;
}
当我不使用JSON.parse()并在Javascript的最后一行写container.innerHTML = response;
时
然后我得到了db:
[{"0":"1","ID":"1","1":"569f303b99f31","UID":"569f303b99f31","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"2","ID":"2","1":"569f308b900ed","UID":"569f308b900ed","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"3","ID":"3","1":"569f3112d4111","UID":"569f3112d4111","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"4","ID":"4","1":"569f315428ad7","UID":"569f315428ad7","2":"Horvaatia hommik","HEADING":"Horvaatia hommik","3":null,"DESCRIPTION":null},{"0":"5","ID":"5","1":"569f31943eea2","UID":"569f31943eea2","2":"Riias","HEADING":"Riias","3":null,"DESCRIPTION":null},{"0":"6","ID":"6","1":"569f31ea7f62a","UID":"569f31ea7f62a","2":null,"HEADING":null,"3":null,"DESCRIPTION":null},{"0":"7","ID":"7","1":"569f3266b5b81","UID":"569f3266b5b81","2":"Riias","HEADING":"Riias","3":null,"DESCRIPTION":null},{"0":"8","ID":"8","1":"569f332d883d1","UID":"569f332d883d1","2":"Horvaatia trip - Poolas","HEADING":"Horvaatia trip - Poolas","3":null,"DESCRIPTION":null},{"0":"9","ID":"9","1":"569f33b357d2f","UID":"569f33b357d2f","2":"Soneburg","HEADING":"Soneburg","3":null,"DESCRIPTION":null}]
可能是因为我的数组中的空值? 或者是什么?