[英]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.