我正在创建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 (markers) {
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)
您的成功回调函数需要data
,但您引用了markers
。
将.success(function(...){})
从function(data)
更改为function(markers)
。
您的$ http构造中还有一个未定义的data
引用。我不知道这是从哪里来的。
**编辑:**
function httpCall($http) {
$http({
method: 'GET',
url: '<?php echo site_url('User/marks') ?>',
data: data, //This is the issue right here
dataType: "
在此结构中,':'之前的data
是属性。 ':'之后的data
是一个变量。 data
变量从未声明。对于更易于维护/可读的代码,您实际上应该重命名data
变量,以便明确这一点。
例如,
var dataToSendToServer = { } //declare an empty object
然后更新您的httpCall方法:
function httpCall($http) {
$http({
method: 'GET',
url: '<?php echo site_url('User/marks') ?>',
data: dataToSendToServer,
dataType: "json"
})