I ma trying to build an app which gets user data (lat, lon) from the datatabase (mysql) and then show their Position on google map.
我想尝试构建一个从数据库(mysql)获取用户数据(lat,lon)的应用程序,然后在谷歌地图上显示它们的位置。
Here is the code sample :
这是代码示例:
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {lat: 33.7167, lng: 73.0667},
zoom: 11
});
makeRequest('get_locations.php', function(data) {
//alert("abc");
var data = JSON.parse(data.responseText);
//window.alert(data);
for (var i = 0; i < data.length; i++) {
//display(data[i]);
displayLocation(data[i]);
}
});
}
function makeRequest(url, callback) {
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
} else {
request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
}
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback(request);
}
}
request.open("GET", url, true);
request.send();
}
get_locations.php
<?php
include('connection.php');
$l= array();
$result = mysqli_query($con,"SELECT * FROM users");
while ($row = mysqli_fetch_assoc($result)) {
$l[] = $row;
}
$j = json_decode($l, true);
//echo $j;
?>
Some things I have tried to find the issue on the code but failed
有些事情我试图在代码上找到问题但却失败了
-
In get_location.php on echo $j; it shows me the correct response
在echo $ j的get_location.php中;它向我展示了正确的反应
-
Before this line var data = JSON.parse(data.responseText); alert("abc") works but not after this, so I Think the issue resides at this line, But dont know why its happening, Any idea ?
在此行之前var data = JSON.parse(data.responseText);警报(“abc”)有效,但不是在此之后,所以我认为问题存在于这一行,但不知道为什么会发生这种情况,不知道吗?
1 个解决方案
#1
0
I think the main issue is the misuse of json_decode
in a situation where you require the opposite, json_encode
我认为主要问题是在需要相反的json_encode的情况下滥用json_decode
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {lat: 33.7167, lng: 73.0667},
zoom: 11
});
makeRequest('get_locations.php', function(data) {
if( data ){
var data = JSON.parse( data );
for( var n in data ){
var obj=data[ n ];
console.log( '%o', obj );
displayLocation.call(this,obj);
}
}
});
}
function makeRequest(url, callback) {
var request;
request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback( request.responseText );
}
}
request.open("GET", url, true);
request.send();
}
<?php
include('connection.php');
$l= array();
$result = mysqli_query( $con, "SELECT * FROM users" );
while ( $row = mysqli_fetch_assoc( $result ) ) {
$l[] = $row;
}
/* encode array as a json object */
$json = json_encode( $l );
/* send the correct content-type header */
header('Content-Type: application/json');
/* send the data */
exit( $json );
?>
#1
0
I think the main issue is the misuse of json_decode
in a situation where you require the opposite, json_encode
我认为主要问题是在需要相反的json_encode的情况下滥用json_decode
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {lat: 33.7167, lng: 73.0667},
zoom: 11
});
makeRequest('get_locations.php', function(data) {
if( data ){
var data = JSON.parse( data );
for( var n in data ){
var obj=data[ n ];
console.log( '%o', obj );
displayLocation.call(this,obj);
}
}
});
}
function makeRequest(url, callback) {
var request;
request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback( request.responseText );
}
}
request.open("GET", url, true);
request.send();
}
<?php
include('connection.php');
$l= array();
$result = mysqli_query( $con, "SELECT * FROM users" );
while ( $row = mysqli_fetch_assoc( $result ) ) {
$l[] = $row;
}
/* encode array as a json object */
$json = json_encode( $l );
/* send the correct content-type header */
header('Content-Type: application/json');
/* send the data */
exit( $json );
?>