问题
This is what i'm trying to do:
function fetch() {
$http.get("http://www.myapifilms.com/imdb/top")
.success(function(response) {
console.log(response);
$scope.details = response;
});
};
The error i'm receiving is the following:
XMLHttpRequest cannot load http://www.myapifilms.com/imdb/top. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
I've looked up quite a bit on this issue or CORS but still can't seem to realize what i have to add and in which file(s).
回答1:
This means that http://www.myapifilms.com does not allow "localhost" origin to access its data.
I could recommend two solutions:
try using http-server by running
npm install -g http-server
download a chrome plugin(not recommended) but can be handy for testing https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?utm_source=chrome-app-launcher-info-dialog
回答2:
use this plugin: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
//make headers for service
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('appCurrentVersionNumber', AppConstant.APP_VERSION);
headers.append('WebServiceIdentifier', AppStorage.getItem('webserviceidentifier'));
this.headers = {headers}; this.http.post(API_URL + 'checkInVan', JSON.stringify(userData), this.headers).map(res => res.json());
回答3:
For this request to succeed, the response from myapifilms.com would need the correct headers set. What you should do is route your request through a server of your own. However you could get around it for testing purposes with a browser extension
Explained in detail here.
来源:https://stackoverflow.com/questions/34189883/cors-problems-no-access-control-allow-origin-header-is-present-on-the-requeste