我有一個名爲'script1.php'的PHP會話文件,它包含'$ position'數組。我想在javascript文件中訪問這個數組的值 。我嘗試使用這個「$ .get('script1.php',function(data){var x = data;});」 命令和'數據'變量我得到{「經度」:12.917186,「緯度」:50.831356}這種結果,但我需要訪問 只有整數,我如何訪問整數值。我已經上傳了php代碼。可有人請幫助我如何 解決這個問題,我是新來的PHP。如何在javascript中訪問php會話文件變量或數據?
<?php
// Start the session
session_name("mysession");
session_start();
if(isset($_SESSION["i"])){
$i = $_SESSION["i"];
}
else{
$i = 0;
}
// Set session variables
$position = array
(
array(50.831118, 12.916574),
array(50.831118, 12.918574),
array(50.831356, 12.917186),
array(50.831604, 12.917834),
array(50.831763, 12.918172),
array(50.831916, 12.918603),
array(50.832825, 12.918413),
array(50.833206, 12.918303),
array(50.832825, 12.918413),
array(50.831916, 12.918603),
array(50.831763, 12.918172),
array(50.831604, 12.917834),
array(50.831356, 12.917186),
array(50.831118, 12.918574),
array(50.831118, 12.916574)
);
echo '{"Longitude": '.$position[$i][1].', "Latitude": '.$position[$i]
[0].'}';
$i++;
if($i > count($position)){
$i = 0;
}
$_SESSION["i"] = $i;
JavaScript代碼存儲在數據變量 { 「經度」:12.917186, 「緯度」:50.831356}
$.get('script1.php', function (data)
{
var x = data;
});
值
您需要解碼JSON字符串轉換爲對象,如JSON.parse(data) –