【发布时间】:2018-07-03 05:34:00
【问题描述】:
我正在尝试从天气 API 获取天气数据。此 API 需要位置的经度和纬度。因为我想使用位置名称(经纬度)作为用户输入,所以我想首先使用地理编码 API 获取这些值。
但是当我尝试在地理编码 API 的 ajax 请求中调用天气 API 时,我得到了一个跨域错误。
代码如下:
function get_gps_weather(location,maps_api_key, weather_api_key){
$.ajax({
url : 'https://maps.googleapis.com/maps/api/geocode/json?address='+location+'&key='+api_key,
type: 'GET',
success : function(data){
var latitude=data['results'][0]['geometry']['location']['lat'];
var longitude=data['results'][0]['geometry']['location']['lng'];
$.ajax({
url : 'https://api.darksky.net/forecast/'+weather_api_key+'/'+latitude+','+longitude
type: 'GET',
success : function(weather_data){
console.log(weather_data)
}
});
}
});
}
谢谢, 卢卡斯
【问题讨论】:
-
具体错误是什么?
-
跨域错误与嵌套的 ajax 调用无关。我认为您如何嵌套它们没有问题。尝试自己调试每个 ajax 调用,当您获取的服务器没有响应跨域标头时会出现跨域错误。
标签: javascript jquery ajax