我正试图在角度工厂使用$ http电话从'worldweatheronline'提取天气信息:
this.testApi = function(coords) {
var deferred = $q.defer();
$http.jsonp(API_ROOTS + '?key=9834687w634087623eg8932te&q=' + coords.latitude + ',' + coords.longitude + '&cc=yes&includeLocation=yes&format=json')
.then(function(response) {
deferred.resolve(response.current_condition);
console.log(response.current_condition);
}, function(error) {
deferred.reject(error);
}
);
return deferred.promise;
};
和控制器:
$scope.updateLocalWeather = function() {
$geolocation.get().then(
function(position) {
$weather.testApi(position.coords).then(
function(weather) {
$scope.localWeather = weather;
$ionicSlideBoxDelegate.update();
}
);
}
);
};
并从控制台显示错误:
Uncaught SyntaxError: Unexpected token :
但是当我在console.log:
时,响应就会响起{ "data": { "current_condition": [ {"cloudcover": "0", "FeelsLikeC": "11", "FeelsLikeF": "51", "humidity": "42", "observation_time": "07:29 AM", "precipMM": "0.0", "pressure": "1029", "temp_C": "11", "temp_F": "51", "visibility": "10", "weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ], "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], "winddir16Point": "SW", "winddirDegree": "235", "windspeedKmph": "4", "windspeedMiles": "2" } ], "nearest_area": [ { "areaName": [ {"value": "Randjesfontein" } ], "country": [ {"value": "South Africa" } ], "latitude": "-25.952", "longitude": "28.143", "population": "0", "region": [ {"value": "Gauteng" } ], "weatherUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/v2\/weather.aspx?q=-25.9484274,28.1395815" } ] } ], "request": [ {"query": "Lat -25.95 and Lon 28.14", "type": "LatLon" } ], "weather": [ { "astronomy": [ {"moonrise": "09:08 PM", "moonset": "08:47 AM", "sunrise": "06:45 AM", "sunset": "05:43 PM" } ], "date": "2015-08-03", "hourly": [ {"chanceoffog": "0", "chanceoffrost": "0", "chanceofhightemp": "0", "chanceofovercast": "0", "chanceofrain": "0", "chanceofremdry": "0", "chanceofsnow": "0", "chanceofsunshine": "100", "chanceofthunder": "0", "chanceofwindy": "0", "cloudcover": "0", "DewPointC": "-3", "DewPointF": "26", "FeelsLikeC": "9", "FeelsLikeF": "48", "HeatIndexC": "9", "HeatIndexF": "47", "humidity": "43", "precipMM": "0.0", "pressure": "1028", "tempC": "9", "tempF": "47", "time": "200", "visibility": "10", "weatherCode": "113", "weatherDesc": [ {"value": "Clear" } ]}
真的不确定我在这里做错了什么。任何帮助将不胜感激。
答案 0 :(得分:1)
您可以解析JSON以获得更好的结果:
$scope.localWeather = JSON.parse(weather);
但我用JSON lint检查了你的json输出,结果是JSON错了。 您是否可以在开发者控制台中查看相同的JSON是否来自您输出的气象服务?如果是,那么api就是一个问题,你无法做任何事情。
答案 1 :(得分:0)
Gah犯了一个愚蠢的错误,我没想到这个问题的时间太长了...错误发生了,因为我忘记在我的jsonp调用结束时添加'callback=JSON_CALLBACK'
...如果我在这里留下这个答案它会帮助其他人......