I need to send a json array from a php script to Javascript.
我需要将一个json数组从php脚本发送到Javascript。
<?php
//controllo se sono presenti i parametri
if(isset($_GET['ID_utente']) && isset($_GET['Longitudine']) && isset($_GET['Latitudine']))
{
//Recupero il valore dei parametri
$ID_utente = $_GET['ID_utente'];
$Longitudine = $_GET['Longitudine'];
$Latitudine = $_GET['Latitudine'];
}
//eseguo la connessione al database sul server locale
//inserendo nome utente e password
$conn = mysql_connect('localhost', 'realegr', 'rabredugbo19');
//gestione degli errori
// if (!$conn) {die('Impossibile connettersi: ' . mysql_error());}
//seleziono il databse
mysql_select_db("my_realegr") or die( "Impossibile selezionare il database.");
//creo una stringa sql di inserimento con i valori
//recuperati dall'url
$sql = "INSERT INTO `my_realegr`.`DatiSinistro`
(
`ID_sinistro` ,
`Tempo_Server` ,
`Tempo_Locale` ,
`ID_utente`,
`Longitudine`,
`Latitudine`
)
VALUES
(
NULL , CURRENT_TIMESTAMP , NULL , '" . $ID_utente . "', '" . $Longitudine . "', '" . $Latitudine . "')
";
$q = "SELECT Longitudine, Latitudine FROM DatiSinistro ORDER by ID_sinistro
DESC LIMIT 1";
$result = mysql_query($q, $conn);
if($row = mysql_fetch_assoc($result)) {
echo json_encode([
'status' => true,
'latitude' => $row['Latitudine'],
'longitude' => $row['Longitudine'],
]);
}
//gestione degli errori
if(! $result ){die('Impossibile eseguire la query: ' . mysql_error());}
//chiudo la connessione al db
mysql_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBkEtPH07_xBVh8dWBzCQYtWimkOUnPGMQ&callback=initMap"></script>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap(){
var $request = $.get('http://realegr.altervista.org/ProvaReale1.php');
$request.done( function(data) {
alert(data);
var pasedData = JSON.parse(data);
alert(pasedData.latitude);
var uluru = {lat: pasedData.latitude, lng:pasedData.longitude};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
})
}
</script>
</body>
</html>
php脚本的输出:
When I load Maps,alert(data) shows me : {"status":true,"latitude":"45.062583","longitude":"7.662160"}
当我加载地图时,alert(数据)显示:{“状态”:true,“latitude”:“45.062583”,“经度”:“7.662160”
So PHP script seems work well and besides Javascript receives JSON array With "var pasedData = JSON.parse(data); " do I obtain an array parsed?? When I try to show the content with alert(pasedData[0]) it shows me "undefined".What's wrong?
因此,PHP脚本似乎运行得很好,除了Javascript外,它还接收到带有“var pasedData = JSON.parse(data)”的JSON数组;“我得到一个解析的数组吗?”当我试图用alert(pasedData[0])显示内容时,它会显示“未定义”。怎么了?
2 个解决方案
#1
1
use pasedData.status
, pasedData.latitude
and pasedData.longitude
使用pasedData。地位,pasedData。纬度和pasedData.longitude
UPDATED
更新
Please use this code
请使用此代码
<script>
function initMap(){
//
}
</script>
and place your code of initMap
inside this function.
并将你的initMap代码放在这个函数中。
UPDATE 2
更新2
Please use this code
请使用此代码
var pasedData = JSON.parse(data);
var longi = parseFloat(pasedData.longitude);
var lati = parseFloat(pasedData.latitude);
var uluru = {lat: lati, lng:longi};
#2
0
Access with .
operator in place of trying it like array index. It will Work. Can try below example in browser console.
访问。操作符代替了数组索引。它将工作。可以试试下面的例子在浏览器控制台。
var data = '{"status":true,"latitude":"45.062583","longitude":"7.662160"}';
var t = JSON.parse(data);
console.log(t.status);
#1
1
use pasedData.status
, pasedData.latitude
and pasedData.longitude
使用pasedData。地位,pasedData。纬度和pasedData.longitude
UPDATED
更新
Please use this code
请使用此代码
<script>
function initMap(){
//
}
</script>
and place your code of initMap
inside this function.
并将你的initMap代码放在这个函数中。
UPDATE 2
更新2
Please use this code
请使用此代码
var pasedData = JSON.parse(data);
var longi = parseFloat(pasedData.longitude);
var lati = parseFloat(pasedData.latitude);
var uluru = {lat: lati, lng:longi};
#2
0
Access with .
operator in place of trying it like array index. It will Work. Can try below example in browser console.
访问。操作符代替了数组索引。它将工作。可以试试下面的例子在浏览器控制台。
var data = '{"status":true,"latitude":"45.062583","longitude":"7.662160"}';
var t = JSON.parse(data);
console.log(t.status);