嘿伙计们我有geolocation.html文件和test.php文件。我使用Ajax将var经度和纬度从geolocation.html传递给我的test.php进行处理,但它显示的是空白页面。我不知道为什么?请帮忙 geolocation.html
<html>
<head>
<script type= "text/javascript" src= "js/jquery.js" > </script>
<script>
//javascript is on the client
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
document.write("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//Pass longitude and latitude to php how?
$.ajax({
type: 'POST',
url: 'test.php',
data: {lat: latitude,
lng:longitude}
});
}
</head>
然后我的test.php
<?php
// $addlat = $_GET['addlat'];
// $addlong = $_GET['addlong'];
if (isset($_POST['lat']) && isset($_POST['lng']) ) {
$lat = $_POST['lat'];
$lng = $_POST['lng'];
echo $lat." ".$lng ;
}
?>
答案 0 :(得分:0)
我修改了你的geolocation.html文件。希望这会有所帮助!
<html>
<head>
//ajax lib file
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
//javascript is on the client
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
document.write("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//Pass longitude and latitude to php how?
$.post("test.php",
{
lat: latitude,
lng: longitude
},
function(data){
alert(data);
});
}
</script>
</head>
<button onclick="getLocation();">GET LOCATION</button>