簡體   English   中英

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錯誤。 您能否在開發者控制台中檢查是否與您輸出的相同的JSON來自氣象服務? 如果是,則說明api存在問題,您無法執行任何操作。

Gah犯了一個愚蠢的錯誤,我沒有考慮足夠長的時間...發生此錯誤是因為我忘記在jsonp調用的末尾添加'callback=JSON_CALLBACK' ...如果有幫助,它將在此處保留此答案還有誰...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM