PHP & jQuery "$.post“正在制造麻烦。我在PHP文件中发布了几个变量。
jQuery是:
navigator.geolocation.getCurrentPosition(saveGeoLocation);
function saveGeoLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.post("__g.php", {
latitude: latitude,
longitude: longitude
}).done(function(data) {
console.log("Data Loaded: " + data);
});
}PHP是:
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "Position output: " . $latitude . $longitude;控制台使用通过jQuery发送的所有信息正确地显示回波。然而,在页面本身上,PHP只是回显引号中的内容,而不是变量内容。
PHP位于一个文件中,但通过include()导入到更大的文件中。
(这是一个简化的例子,可能有一个错误。)
谢谢你的智慧!
----------EDIT--------:
我的问题可能是因为菜鸟的错误。是否可以在包含php文件的同时向其发送数据,以便将数据输出到您想要的位置?比如:
<somehtml>
<somejquery> *--> is retrieving Geolocation-coordinates and posting long/lat to "__g.php"*
<?php
<somephp>
include("__g.php"); *--> is echoing the full API-url containing the long/lat values from the jQuery-post*
<someforeach> *--> for the received API-json*
?>发布于 2018-09-11 13:59:48
更改以下内容:
$.post("__g.php", {
latitude: latitude,
longitude: longitude
}).done(function(data) {
console.log("Data Loaded: " + data);
});至
$.ajax({
url: "__g.php",
type: "POST",
dataType "text",
data: {
latitude: latitude,
longitude: longitude
},
success: function(data) {
console.log("Data Loaded: " + data);
},
error: function(data) {
console.log("Error: an error occurred somewhere");
}
});从以下位置更改php代码:
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "Position output: " . $latitude . $longitude;至
if (isset($_REQUEST['latitude']) && isset($_REQUEST['longitude'])) {
$latitude = $_REQUEST['latitude'];
$longitude = $_REQUEST['longitude'];
print "Position output: " . $latitude . $longitude;
} else {
header('HTTP/1.1 500 Internal Server');
header('Content-Type: application/json; charset=UTF-8');
}https://stackoverflow.com/questions/52277411
复制相似问题