Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs access local json files [duplicate]

I am new to angularjs and I've been searching on the web the whole day just to find a solution in getting data from a local json file without having to use a localhost for my webapp. Unfortunately, I haven't found any. I tried using $http.get but I get Cross Origin * error.

Is there no other way that I could get the data from my local json file without having to locally host my webapp?

Does angularjs have any other function to get data from local json files without having to use its $http.get?

like image 256
Jibo Avatar asked Oct 30 '14 09:10

Jibo


2 Answers

You can refer to json file calling them with a GET method, like so:

$http.get('phones/phones.json').success((data) => {
   $scope.phones = data;
});

Check the official AngularJS tutorial to see what you are doing wrong.

Update:

For angular 1.6+ success method is not valid anymore instead use then:

$http.get('phones/phones.json').then((data) => {
   $scope.phones = data;
});

Refer to source here.

like image 112
Kutyel Avatar answered Sep 25 '22 07:09

Kutyel


You need to deploy your application to some http server, the easiest way to this is using node, here is the exmaple https://www.npmjs.org/package/http-server, or you can change you browser security settings.

like image 39
Grissom Avatar answered Sep 23 '22 07:09

Grissom