繁体   English   中英

Ajax PHP 将变量放在字符串中以进行 API 调用

[英]Ajax PHP placing variables in string for API call

我已经阅读并尝试实施我在网上找到的所有解决方案,但直到现在我无法让它工作,所以是时候寻求帮助了:

我将以下 Ajax 调用发送到我的 PHP 文件:

        $.ajax({
            url: "libs/php/geolocate.php",
            type: 'POST',
            dataType: 'json',
            data: {
                lat: $lat,
                lon: $lon
            },

这会在 php 文件中调用以下调用:

$result = $geocoder->geocode( '51.952659, 7.632473' );

一切正常,但我接下来要做的是用我在 ajax 调用中发送的那些替换预先格式化的坐标。 这是我失败的地方,我尝试了不同的方法来注入 lat 和 lon,包括来自先前项目的逻辑: $_REQUEST['lat'] ,似乎没有任何效果。

有人可以建议我需要使用什么格式吗?

非常感谢

编辑 var_dump() 输出:

<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\geonamesExample\libs\php\geolocate.php:2:</small>
<b>array</b> <i>(size=0)</i>
  <i><font color='#888a85'>empty</font></i>
</pre>{"results":[{"source":"opencage","formatted":"Friedrich-Ebert-Straße 7, 48153 Münster, Germany","geometry":{"lat":51.9526599,"lng":7.632473},"countryCode":"DE","timezone":"Europe\/Berlin","roadinfo":{"sideOfRoad":"right","speed":"km\/h"},"currency":{"name":"Euro","symbol":"€"}}]}```

您可以使用$_POST['lat']$_POST['lon']

$result = $geocoder->geocode( $_POST['lat'] . ', ' . $_POST['lon']);

通常php会自动传递带有标题的变量

应用程序/x-www-form-urlencoded

要使用当前设置检索值,您需要使用

php://输入

IE

$data = file_get_contents('php://input') ;

var_dump($data); // associative array with your variables

更多信息在这里

PHP“php://input”与$_POST

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM