我正在创建Google地图应用程序。我需要使用Ajax从数据库加载位置数据,因此我可以每隔x秒显示这些数据。我正在使用CodeIgniter / PHP。这就是我得到的:
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
httpCall();
}
function httpCall($http) {
$http({
method: 'GET',
url: '<?php echo site_url('User/marks') ?>',
data: data,
dataType: "json"
})
.success(function (data) {
for (var i = 0, len = markers.length; i < len; ++i) {
var myLatLng = {lat: markers[i].lat, lng: markers[i].lng};
var marker = new google.maps.Marker({
position: {
lat: parseFloat(markers[i].lat),
lng: parseFloat(markers[i].lng)
},
map: map
});
}
})
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
</body>
</html>
用户/标记功能是:
function marks() {
echo json_encode($this->user_model->get_marks());
}
user_model获取标记功能是:
function get_marks() {
$sql = "select lat,lng
from tbl_locations";
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
我的错误是:
ReferenceError: data is not defined
显然我错误地传递了数据,但是正确的方法是什么?
答案 0 :(得分:0)
将json结果存储在数组中
function marks() {
echo json_encode(array('data'=>$this->user_model->get_marks()));
}
并尝试使用ajax结果中的console.log(data)打印数据结果。
答案 1 :(得分:0)
那是因为你没有定义数据变量,你要通过数据:数据,你需要定义它在函数httpCall($ http)中然后在ajax函数中传递它