我正在尝试将地理位置数据写入HTML文件,但不起作用。我总是收到有关数据的以下信息:
“注意:未定义的索引:”
jquery变量数据始终为空
......我做错了什么?
提前致谢。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<body onload="testResults()">
<script>
function testResults()
{
if (navigator.geolocation)
{
//Get the current position
navigator.geolocation.getCurrentPosition(function (position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//var cadena = "lat=" + latitude + "&lon=" + longitude;
var cad = latitude + " " + longitude;
// do callback
$.ajax(
{
type: "POST",
url: "checklo.php",
//data: "lat=" + latitude + "&lon=" + longitude
data: cad,
}).done(function (msg)
{
// Only to check it
alert("Data Saved: " + cad);
});
});
}
else
{
alert("Sorry... your browser does not support the HTML5 GeoLocation API");
}
}
和php文件的代码checklo.php
<?php
$file = 'geo.html';
$datos = $_POST['data'];
if (isset($datos)) {
$fp = fopen($file, "a");
fputs($fp, "</br>$datos </br>");
flock($fp, 3);
fclose($fp);
//echo 'msg ';
}
else {
//echo 'msg !';
}
?>
答案 0 :(得分:0)
谢谢你的回答
我用print_r($ _ POST,true)和var_export($ _ POST,true)检查了它 我没有输出,我在html文件中唯一得到的就是这个,一个空的 阵列:
</br>Array
(
)
</br></br>Array
(
)
</br></br>Array
(
)
我尝试了两种使用数据的方法但是没有工作
$.ajax(
{
type: "POST",
url: "checklo.php",
//data: "lat=" + latitude + "&lon=" + longitude ,
data: 'cad' ,
}).done(function (msg)
答案 1 :(得分:0)
我已经能够在ajax调用
中使用以下格式解决了data: { lat: latitude, lon: longitude }
和checklo.php
foreach ($_POST as $key => $value)
$dat .= $key . ' -> ' . $value . '<br>';
谢谢