我已经更新了下面的代码,并在进一步研究后将我的用户名添加到地名请求中,但仍然没有乐趣。
可能是我试图从 HTML 传递的值,所以我现在也包括在内。
<td>1. Find Nearest Ocean</td>
<td>
<select id="latitudeOne">
<option>40.78343</option>
<option>133.7751</option>
<option>106.3468</option>
</select>
<select id="longitudeOne">
<option>-43.96625</option>
<option>25.2744</option>
<option>56.1304</option>
</select>
</td>
<td><button id="btnRunOne">Run</button></td>
</tr>
问题似乎在 ajax 请求和从 geonames API 获取数据请求之间
$('#btnRunOne').click(function() {
$.ajax({
url: "php/ocean.php",
type: 'POST',
dataType: 'json',
data: {
lat: $('#latitudeOne').val(),
lng: $('#longitudeOne').val()
},
success: function(result) {
console.log(JSON.stringify(result));
if (result.status.name == "ok") {
$('#txtDistance').html(result['data'][0]['distance']);
$('#txtGeonameID').html(result['data'][0]['geonameid']);
$('#txtName').html(result['data'][0]['name']);
}
},
error: function(jqXHR, textStatus, errorThrown) {
// your error code
}
});
和我需要发回数据的php。我收到以下错误
警告:第27行C:\xampp\htdocs\geonamesExample\libs\php\getCountryInfo.php中未定义的数组键“geonames”
这是 $output['data'] = $decode['geonames'];
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$executionStartTime = microtime(true);
$url='http://api.geonames.org/oceanJSON?lat=' . $_REQUEST['lat'] . '&lng=' . $_REQUEST['lng'] . '&username=simonjadams';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$decode = json_decode($result,true);
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['status']['returnedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";
$output['data'] = $decode['geonames'];
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);
?>
有什么很明显的我想念的吗?
这是我试图从中获取信息的 GeoNames 服务:
海洋/反向地理编码 海洋或海洋的名称。网络服务类型:REST 网址:api.geonames.org/ocean? 参数:lat、lng、radius(可选) 结果:返回给定纬度/经度的海洋或海洋 此处列出了服务返回的海洋。示例http://api.geonames.org/ocean?lat=40.78343&lng=-43.96625&username=demo
此服务也提供 JSON 格式:api.geonames.org/oceanJSON?lat=40.78343&lng=-43.96625&username=demo