我正在使用PHP脚本将JSON数据发送到我的网站。我正在使用javascript尝试迭代我的json数据,以便我可以将信息存储在数组中。
line var length = emails.length给出了电子邮件是对象的错误
<script>
var emails;
$.get("scripts/getUsers.php", function(data) {
for(element in data.email) {
emails.push(element);
};
});
var length = emails.length;
alert(length);
for(i = 0; i < length; i++) {
$('<div/>', {
'class':'col-md-3 col-xs-12 widget widget_tally_box',
'html':'<p>Hello' + i + '</p>'
}).appendTo("body");
}
</script>
这是PHP脚本
<?php
require_once '../includes/DBCredentials.php';
$response = array();
if($_SERVER['REQUEST_METHOD'] == 'GET') {
$db = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
$statement = $db->query("SELECT email FROM user");
while($row = $statement->fetch_assoc()) {
$email = $row['email'];
$response['email'] = $email;
}
echo json_encode($response);
}
?>