worldweatheronline API 响应错误

worldweatheronline API response error

我正在尝试使用 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;
    };

和控制器:

$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 是错误的。 如果 The same JSON comes from weather service as you output,你能在开发者控制台检查一下吗?如果是,那是 api 的问题,您无法对其进行任何操作。

Gah 犯了一个愚蠢的错误,我对这个问题的思考时间不够长...发生错误是因为我忘记在我的 jsonp 调用末尾添加 'callback=JSON_CALLBACK'...将留下这个答案如果它对其他人有帮助...