从 json 获取某些数据(google 地理编码 api)
Get certain data from json (google geocode api)
我尝试通过 google 地理编码 api.json 请求获取某些数据。
我的代码如下所示:
$('#test').on('focusout', function() {
var address = $('#ustreet').val() + " " + $('#ustreetnr').val() + ", " + $('#uplz').val();
$('#uaddress').val(address);
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address,
dataType: 'jsonp',
success: function(json) {
console.log(json.geometry.location.lat);
console.log(json);
}
});
});
我的请求结果很好,看起来像这样:
http://maps.google.com/maps/api/geocode/json?address=Celler%20Weg%2055,%2021079
但是我在从 geometry->location 获取 "lat & lng" 值并将其存储到变量中时遇到了问题。
ReferenceError: json is not defined
我肯定是一个 JavaScript/jQuery 初学者,但对于另一个 API 的项目,它的效果与我在这里尝试的方式相同...
希望能在这里得到一些帮助:)
来自波兰的问候
将 dataType: 'jsonp'
更改为 dataType: 'json'
。
JSONP stands for JSON with Padding.
dataType: jsonp
用于跨域请求,即请求到不同的域。 dataType: json
同域同源请求。
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=Prunesstraat 4 opheusden',
dataType: 'json',
success: function(json) {
console.log(json);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我尝试通过 google 地理编码 api.json 请求获取某些数据。
我的代码如下所示:
$('#test').on('focusout', function() {
var address = $('#ustreet').val() + " " + $('#ustreetnr').val() + ", " + $('#uplz').val();
$('#uaddress').val(address);
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address,
dataType: 'jsonp',
success: function(json) {
console.log(json.geometry.location.lat);
console.log(json);
}
});
});
我的请求结果很好,看起来像这样:
http://maps.google.com/maps/api/geocode/json?address=Celler%20Weg%2055,%2021079
但是我在从 geometry->location 获取 "lat & lng" 值并将其存储到变量中时遇到了问题。
ReferenceError: json is not defined
我肯定是一个 JavaScript/jQuery 初学者,但对于另一个 API 的项目,它的效果与我在这里尝试的方式相同...
希望能在这里得到一些帮助:)
来自波兰的问候
将 dataType: 'jsonp'
更改为 dataType: 'json'
。
JSONP stands for JSON with Padding.
dataType: jsonp
用于跨域请求,即请求到不同的域。 dataType: json
同域同源请求。
$.ajax({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=Prunesstraat 4 opheusden',
dataType: 'json',
success: function(json) {
console.log(json);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>