Jas*_*son 4 mysql ajax jquery responsetext
我有一个数据库表,其字段包含位置的纬度和经度坐标.我想使用数据库中的信息为Google Map视图创建标记.
我已经将查询函数作为
function getCords(){
$link = connectDB();
$query = "SELECT * FROM tour";
$results = mysqli_query($link, $query);
$jsonArray = array();
while ($row = mysqli_fetch_assoc($results)){
$jsonArray[] = array('fileName' => $row['FileName'], 'lat' => $row['Lat'], 'lon' => $row['Lon']);
}
return json_encode($jsonArray);
}
Run Code Online (Sandbox Code Playgroud)
当我从php页面调用此函数时,它返回通常的JSON格式.
我的问题是执行ajax查询.我在上面的php脚本文件中有查询功能,该文件包含六个左右的实用程序函数,用于控制登录,注销,注册等.为了通过jquery查询数据库,我试过了
var request = $.ajax({
type:"GET",
url: "includes/phpscripts.php?action=cords",
type: "json"
});
var response = request.responseText;
Run Code Online (Sandbox Code Playgroud)
我的问题是响应总是空的.这是由于URL的形成还是由于其他原因造成的?
$.ajax({
type:"GET",
url: "includes/phpscripts.php?action=cords",
dataType: 'json', // necessary, because you're sending json from server
success: function(response) { // response will catch within success function
console.log(response);
}
});
Run Code Online (Sandbox Code Playgroud)
要么
var request = $.ajax({
type:"GET",
url: "includes/phpscripts.php?action=cords",
dataType: 'json', // necessary, because you're sending json from server
}).done(function(response) {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
而不是return json_encode($jsonArray);,使用echo json_encode($jsonArray);