阻止从 Open Weather 加载资源

Blocked loading resource from Open Weather

处理 AngularJs 应用程序并通过 $resource 调用 Open Weather API,面临以下问题。

Blocked loading resource from url not allowed by $sceDelegate policy. URL: http://samples.openweathermap.org/data/2.5/forecast

代码

$scope.weatherAPI = $resource('http://samples.openweathermap.org/data/2.5/forecast',
    { callback: 'JSON_CALLBACK' },
    { get: { method: 'JSONP' } }
);

$scope.weatherResul = $scope.weatherAPI.get({ q: $scope.srchCityName

任何想法,如何解决?

我认为您必须启用 URL 作为可信资源。我从不使用 $resource,所以机制可能有点不同,但基本上你必须注入 $sce 然后调用 $sce.getTrustedResourceUrl('http://samples.openweathermap.org/data/2.5/forecast')。使用 $resource 你可以这样做:

$scope.weatherAPI = $resource($sce.getTrustedResourceUrl('http://samples.openweathermap.org/data/2.5/forecast'),
    { callback: 'JSON_CALLBACK' },
    { get: { method: 'JSONP' } }
);

如果这不起作用,请查看此答案:$sce.trustAsResourceUrl() globally 以获得全局白名单解决方案。

您甚至不需要这样做,只需删除 CALLBACK 和 JSONP

$resource("http://api.openweathermap.org/data/2.5/weather/?APPID=YOURAPPID");