简体   繁体   English

杰森不使用angular.js

[英]Json not working with angular.js

I'm trying to get forecast information from an external domain, which doesn't seem to work yet. 我正在尝试从外部域获取预测信息,但这似乎还行不通。 Both Angular and jQuery are loaded, but nothing shows up in HTML. Angular和jQuery均已加载,但HTML中未显示任何内容。 Chrome doesn't report an error message either. 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;
            });
        }

Since you are updating the scope after a callback which is outside the angular execution context, you need to use $scope.$apply() . 由于要在角度执行上下文之外的回调之后更新作用域,因此需要使用$scope.$apply()

But as others have mentioned in their comments $http can be used, with JSONP as the request is cross domain. 但是正如其他人在评论中提到的那样,可以使用$http和JSONP,因为请求是跨域的。

I think same-origin policy forbids you from performing cross-domain query. 我认为同源策略禁止您执行跨域查询。 What you can do is to perform the query on the server side, and return the result to the browser. 您可以在服务器端执行查询,然后将结果返回到浏览器。 Another workaround is to use jsonp. 另一个解决方法是使用jsonp。 But it is not recommended due to its insecure nature. 但由于其不安全的性质,不建议使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM