首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery to PHP $.post不是回显页面上的任何内容,而是将数据回显到jQuery

jQuery to PHP $.post不是回显页面上的任何内容,而是将数据回显到jQuery
EN

Stack Overflow用户
提问于 2018-09-11 13:44:46
回答 1查看 91关注 0票数 0

PHP & jQuery "$.post“正在制造麻烦。我在PHP文件中发布了几个变量。

jQuery是:

代码语言:javascript
复制
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是:

代码语言:javascript
复制
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "Position output: " . $latitude . $longitude;

控制台使用通过jQuery发送的所有信息正确地显示回波。然而,在页面本身上,PHP只是回显引号中的内容,而不是变量内容。

PHP位于一个文件中,但通过include()导入到更大的文件中。

(这是一个简化的例子,可能有一个错误。)

谢谢你的智慧!

----------EDIT--------:

我的问题可能是因为菜鸟的错误。是否可以在包含php文件的同时向其发送数据,以便将数据输出到您想要的位置?比如:

代码语言:javascript
复制
<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*
?>
EN

回答 1

Stack Overflow用户

发布于 2018-09-11 13:59:48

更改以下内容:

代码语言:javascript
复制
   $.post("__g.php", { 
  latitude: latitude, 
  longitude: longitude 
  }).done(function(data) {
     console.log("Data Loaded: " + data);
  });

代码语言:javascript
复制
   $.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代码:

代码语言:javascript
复制
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
echo "Position output: " . $latitude . $longitude;

代码语言:javascript
复制
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');
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52277411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档