我一直在尝试从mysql表中检索数据。我的php脚本如下:
$mysqli = mysqli_connect($servername, $username, $password, $dbname);
$query = "SELECT * FROM $tablename";
$result = mysqli_query($mysqli, $query);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$jsonData = json_encode($data);
echo($jsonData);
当从浏览器调用php url时,它会给我这个结果:
[{"id":"p1","image_data":"data:image/jpeg;base64,largestring","image_status":"","image_returnedbit":""},{"id":"p2","image_data":"data:image/jpeg;base64,largestring","image_status":"","image_returnedbit":""}]
(我在mysql表中只有两行。数组中多余的两列不通过atm处理(我很难利用这些数据。我有这个html文件(包含一些css,html,但不包括在内(:
$(function () {
$("#button-get").click(function () {
$.ajax({
type: "GET",
url: "https://www.example.com/myphpfile.php",
dataType: "",
success: function(response){
// alert(response);
// alert(response["id"]);
console.log(response[0]);
// $(".image-gallery").append(`<div class="image-single"><img src=${}></div>`);
}
});
});
});
似乎有一个数组正在被回显,但记录response(0)
会得到[
,记录response(1)
会得到{
。如果有人能帮我:(我真的很感激。感谢您的帮助。以前,我在php脚本上尝试过echo($data)
,但它只在浏览器和/控制台日志上显示了一个单词Array
。我相信echo($jsonData)
正在发送一个字符串,我必须使用它来获取值(拆分和删除某些字符(。我如何实现检索可以在html页面上使用的数据?
这是因为ajax将响应视为字符串,而不是json,所以您有两个选项,要么使用JSON.parse(response)
将json字符串转换为json对象,要么需要将dataType
设置为json