This is my code:
MyAppModule.factory('EventData', function($http,$log){
return {
getEvent : function(successcb){
$http({method: 'GET', url: './js/Services/products.json'}).
success(function(data) {
$log.info("success");
}).
error(function(data) {
$log.info("error");
});
}
};
});
I have a simple JSON file in a local location, and I am trying to read it using the http
method of AngularJS. I am getting the following error:
XMLHttpRequest cannot load file:///C:/Users/Avraam/Documents/GitHub/AngularJS/app/js/Services/products.json Cross origin requests are only supported for HTTP. angular.min.js:73 Error: A network error occurred.
What is my mistake? I am not using any server; I am just openning my index file with Chrome. Is this the mistake? Should I use a server if I want to use the http
method?
If this is for local development and you are using Chrome, you need to run Chrome with a couple of arguments to relax security like this:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files --disable-web-security
I fixed this problem by running my page off a server, like this
cd /path/to/dir/with/the/index/file
python3 -m http.server
then open http://localhost:8000 in your browser.
For other ways of serving the current directory, see this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With