簡體   English   中英

傑森不使用angular.js

[英]Json not working with angular.js

我正在嘗試從外部域獲取預測信息,但這似乎還行不通。 Angular和jQuery均已加載,但HTML中未顯示任何內容。 Chrome也不會報告錯誤消息。

        var currentCityread = "Eindhoven";
        var api1 = 'http://api.openweathermap.org/data/2.5/forecast/daily?q='+currentCityread+'&cnt=10&mode=json&units=metric&APPID=000000000000000&callback=?';
        var api2 = 'http://api.openweathermap.org/data/2.5/weather?q='+currentCityread+'&mode=json&units=metric&APPID=000000000000000&callback=?'
        function ReadJson($scope) {
            $.ajax({
              dataType: "json",
              url: api1,
              }).done(function (output) {
                $scope.jsonTotal = output.list[0].temp.day+output.list[1].temp.day+output.list[2].temp.day+output.list[3].temp.day+output.list[4].temp.day+output.list[5].temp.day+output.list[6].temp.day+output.list[7].temp.day+output.list[8].temp.day+output.list[9].temp.day;
                $scope.jsonCalcu = Math.round($scope.jsonTotal / 10);
                $scope.jsonCurrent = Math.round(output.list[0].temp.day);
                $scope.jsonCurrent2 = Math.round(output.list[1].temp.day);
                $scope.jsonCurrenticon2 = output.list[1].weather[0].icon;
                $scope.jsonCurrent3 = Math.round(output.list[2].temp.day);
                $scope.jsonCurrenticon3 = output.list[2].weather[0].icon;
            });
            $.ajax({
              dataType: "json",
              url: api2,
              }).done(function (outputcurrent) {
                $scope.jsonCurrenticon = outputcurrent.weather[0].icon;
            });
        }

由於要在角度執行上下文之外的回調之后更新作用域,因此需要使用$scope.$apply()

但是正如其他人在評論中提到的那樣,可以使用$http和JSONP,因為請求是跨域的。

我認為同源策略禁止您執行跨域查詢。 您可以在服務器端執行查詢,然后將結果返回到瀏覽器。 另一個解決方法是使用jsonp。 但由於其不安全的性質,不建議使用。

暫無
暫無

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

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