0

I've got this error: Failed to load https://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/47.7510741,-120.7401385: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:10001' is therefore not allowed access. when I was trying to fetch data from this specific URL with $http.get method.

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);
            }
        });
};

What is the easiest way to avoid Access-control error? TY :)

1 Answers1

0

Add this HTTP header to all pages served from your web server listening on localhost:10001

Access-Control-Allow-Origin: *

Specific instructions depend on what server software you are using.

yolabingo
  • 118
  • 1
  • 6