ロードに失敗しましたhttps://api.forecast.io/forecast/4a04d1c42fd9d32c97a2c291a32d5e2d/47.7510741,-120.7401385:要求されたリソースに 'Access-Control-Allow-Origin'ヘッダーがありません。 Origin 'http://localhost:10001'はアクセスできません。 $ http.getメソッドを使用してこの特定のURLからデータを取得しようとしていました。'Access-Control-Allow-Origin'エラーが発生しました
app.js
VARて、myApp = angular.module( "て、myApp"、[])。
myApp.controller( 'myController'、[ '$スコープ'、 '$のhttp'、($スコープ、$ 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);
}
});
};
アクセス制御を回避するための最も簡単な方法は何ですか?すべてのページがローカルホスト上でリスニングWebサーバーから提供さに誤差TY :)
これは、リモートAPIリクエストには役立ちません – charlietfl