简体   繁体   中英

Access-Control-Allow-Origin in angularjs production

In my angularjs application im trying to get some information, in my case longtude and latitude from a rest service online, but is giving me a error of

XMLHttpRequest cannot load http://iatageo.com/getLatLng/OPO. No 'Access-Control-Allow-Origin'
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://iatageo.com/getLatLng/OPO","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

My snipped code is:

$scope.originAirport = 'OPO';
  $http.get('http://iatageo.com/getLatLng/'+$scope.originAirport).then(function(response){
        $scope.originLatLong = response.data;
    });

At first i thougth that was because i was running locally, but after lauching in production i had same issue. Im using angularjs 1.6*

It seems like the API isn't allowing CORS so try to work around it by making your own server that will get the information from the API then pass it to your server.

Again, the API isn't allowing CORS XMLHttpRequest cannot load http://iatageo.com/getLatLng/BSB. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. XMLHttpRequest cannot load http://iatageo.com/getLatLng/BSB. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

If you're familiar with NodeJS Express you can use this script below

const App = require('express')();
const CORS = require('cors');
const RP  = require('request-promise');

const PORT = 3000;

const BASEURL = 'http://iatageo.com/getLatLng/';

App.use(CORS());

App.get('/getLatLng/:airport', function(request, response) {
    const airportCode = request.params.airport;
    const URL = BASEURL + airportCode;
    RP({ url: URL, json: true }).then(function(data) {
        response.json(data);
    });
});

App.listen(PORT, function(err) {
    if (err) throw err;
    console.log('listening to port:', PORT);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM