openweathermap API 请求中的语法错误
Syntax error in openweathermap API request
我在尝试从 'openweathermap'
获取数据时在控制台中收到以下语法错误
Uncaught SyntaxError: Unexpected token :
这是 JS 文件:
var app = angular.module('App', ['ngResource']);
app.factory('weatherService', function($http) {
return {
getWeather: function() {
var weather = '';
// if (!!prmSearchValue) {
// var searchValue = prmSearchValue;
$http.jsonp('https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json').success(function(data) {
weather = 3232;
});
// }
/* else {
weather = {};
} */
return weather;
}
};
});
//Eilat,Israel
app.controller('httpAppCtrlr', function($scope, weatherService) {
$scope.searchText = '';
$scope.searchWeather = function() {
var prmSearchValue = $scope.searchText;
$scope.weather = weatherService.getWeather();
};
});
看起来 returns 的数据在某种程度上被破坏了..
使用 $http Get
代替 JSONP。处理错误的更好方法是使用 .then
,如下更改工厂,
app.factory('weatherService', function ($http) {
return {
getWeather: function () {
var weatherForcast = {};
$http({
method: 'GET',
url: "https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7"
}).then(function successCallback(response) {
angular.extend(weatherForcast, response.data);
}, function errorCallback(response) {
alert('API call failed, possibly due to rate limiting or bad zip code.');
});
return weatherForcast;
}
};
});
在 AngularJS
jsonp
中,您需要将 callback=JSON_CALLBACK
附加到 url。 (我假设您使用 jsonp
而不是 get
是有原因的。)
替换
https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json
和
https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json&callback=JSON_CALLBACK
我在尝试从 'openweathermap'
获取数据时在控制台中收到以下语法错误Uncaught SyntaxError: Unexpected token :
这是 JS 文件:
var app = angular.module('App', ['ngResource']);
app.factory('weatherService', function($http) {
return {
getWeather: function() {
var weather = '';
// if (!!prmSearchValue) {
// var searchValue = prmSearchValue;
$http.jsonp('https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json').success(function(data) {
weather = 3232;
});
// }
/* else {
weather = {};
} */
return weather;
}
};
});
//Eilat,Israel
app.controller('httpAppCtrlr', function($scope, weatherService) {
$scope.searchText = '';
$scope.searchWeather = function() {
var prmSearchValue = $scope.searchText;
$scope.weather = weatherService.getWeather();
};
});
看起来 returns 的数据在某种程度上被破坏了..
使用 $http Get
代替 JSONP。处理错误的更好方法是使用 .then
,如下更改工厂,
app.factory('weatherService', function ($http) {
return {
getWeather: function () {
var weatherForcast = {};
$http({
method: 'GET',
url: "https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7"
}).then(function successCallback(response) {
angular.extend(weatherForcast, response.data);
}, function errorCallback(response) {
alert('API call failed, possibly due to rate limiting or bad zip code.');
});
return weatherForcast;
}
};
});
在 AngularJS
jsonp
中,您需要将 callback=JSON_CALLBACK
附加到 url。 (我假设您使用 jsonp
而不是 get
是有原因的。)
替换
https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json
和
https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json&callback=JSON_CALLBACK