I encountered an error while trying to access a local JSON file. The function I have written to retrieve the data from the JSON is as follows:
$scope.getJSON = function () {
(self.servicioObj = jsonFactory.GetJson('data.json')).then(
function (dataResponse) {
$scope.datosJson = [];
$scope.datosJson = dataResponse.data;
self.servicioObj.detenerTiempo();
}, function (error) {
self.servicioObj.detenerTiempo();
});
}
This is the code for my Factory:
function jsonFactory($http, $q) {
var self = this;
self.urlBase = 'http://192.168.0.17:8383/NewFrontEnd/app/pages/';
self.jsonFactory = {};
self.tiempoEspera = 120000;
self.jsonFactory.GetJson = function (metodo) {
var tiempo = $q.defer();
var url = self.urlBase + metodo;
var peticion = $http({
method: 'get',
url: url,
headers: {'Access-Control-Allow-Origin': '*'},
cache: false,
timeout: tiempo.promise
});
return self.jsonFactory;
}
Upon calling the getJSON function, I receive the following error message:
XMLHttpRequest cannot load http://192.168.0.17:8383/NewFrontEnd/app/pages/data.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access.
I find it strange that I encounter this error when using '192.168.0.17' in the URL instead of 'localhost'. When I change the URL to 'localhost', there are no issues accessing the JSON file.