得到了“访问控制 - 允许 - 来源”#39;错误

时间:2017-12-09 23:20:58

标签: angularjs http get

我收到了这个错误: 无法加载https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/47.7510741,-120.7401385:没有'访问控制 - 允许 - 来源'标头出现在请求的资源上。起源' http://localhost:10001'因此不允许访问。 当我尝试使用$ http.get方法从此特定URL获取数据时。

app.js

var myApp = angular.module(" myApp",[]);

myApp.controller(' myController',[' $ scope',' $ http',($ scope,$ http)=> {

$scope.getWeather = () => {
    var geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${$scope.location}`
    $http.get(geocodeUrl)
        .then((response) => {
            if (response.data.status === 'ZERO_RESULTS') {
                throw new Error('Unable to find that address.');
            }
            var lat = response.data.results[0].geometry.location.lat;
            var lng = response.data.results[0].geometry.location.lng;
            var weatherUrl = `https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/${lat},${lng}`;
            $scope.location = response.data.results[0].formatted_address;
            console.log(response.data.results[0].formatted_address);
            return $http.get(weatherUrl);
        }).then((response) => {
            var temperature = response.data.currently.temperature;
            var apparentTemperature = response.data.currently.apparentTemperature;
            console.log(`It's currently ${temperature}. It feels like ${apparentTemperature}.`);
        }).catch((e) => {
            if (e.code === 'ENOTFOUND') {
                console.log('Unable to connect to API servers.');
            } else {
                console.log(e.message);
            }
        });
};

避免访问控制错误的最简单方法是什么? TY:)

1 个答案:

答案 0 :(得分:0)

将此HTTP标头添加到通过localhost:10001

监听的Web服务器提供的所有页面
Access-Control-Allow-Origin: *

具体说明取决于您使用的服务器软件。

相关问题