javascript - worldweatheronline API 响应错误

标签 javascript angularjs jsonp

我正在尝试使用 Angular 工厂中的 $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;
    };

和 Controller :

$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" } ]}

真的不确定我在这里做错了什么。任何帮助将不胜感激。

最佳答案

您可以解析 JSON 以获得更好的结果:

$scope.localWeather = JSON.parse(weather);

但是我用 JSON lint 检查了你的 json 输出并且说 JSON 是错误的。 您能否在开发者控制台中检查是否与您输出的天气服务相同的 JSON?如果是,那么就是api的问题,你无法做任何事情。

关于javascript - worldweatheronline API 响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31781920/

相关文章:

asp.net-mvc-4 - MVC WebApi + 基础认证 + JSONP跨域

c# - 将 json 中的图像的 base64 字节数组传递给 webApi

javascript - Vapor 3 接收包含 BOOL 的字符串化 JSON

javascript - Ng-model 直到点击页面上的某个地方才显示更新的值

javascript - 在 angularjs 中嵌入

jquery - getJSON 从此 API 返回 XML

javascript - flickr api - 使用 JSONP 获取照片

javascript - 使用 Jquery Isotope 过滤 Fancybox 图库

javascript - react 路由器,redux, Electron : router isn't rendering

javascript - 装饰 Angulars $ExceptionHandler 时如何防止令人讨厌的副作用?