我是php的新手,我使用对php文件的ajax调用来保存用户gps纬度和经度。
echo call in html page returns the coordinates correctly but when I run the php file, it has undefined index
error.
我检查了类似的帖子,发现可能是Apache服务器重写问题,并使用.htacess中的某些代码删除了php扩展名,但我在此类帖子中找不到任何代码。
您能为我提供使用ajax调用将POST数据简单地发送到php文件的正确方法是什么?
这是我的代码
var Latitude;
var Longitude;
getLocation();
function getLocation() {
document.getElementById("img").src = "pic1.jpg";
alert("geolocation");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showposition);
} else {
console.log("GPS not supported");
}
}
function showposition(position) {
Latitude = position.coords.latitude;
Longitude = position.coords.longitude;
$.ajax({
url: 'Location_Data.php',
data: {
'lat': Latitude,
'lng': Longitude
},
type: 'POST',
success: function(result) {
alert("Ajax function");
alert(result);
}
});
}
<img alt="Click Allow" id="img" height="400" width="300">
<p id="demo">Coordinates: </p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
php文件:
<?php
/*
$lat= isset($_POST['lat']) ? $_POST['lat'] : null;
$lng= isset($_POST['lng']) ? $_POST['lng'] : null;
$name= isset($_POST['name']) ? $_POST['name'] : null;
*/
echo "PHP file is live! \r\n";
$lng = $_POST['lng'];
$lat= $_POST['lat'];
echo "\r\n Coordinates: \r\n";
echo $lat;
echo "\r\n";
echo $lng;
?>
我正在显示php文件和控制台的屏幕截图,其中包含一些开发工具警告