$http question


#1

Hi,
I’m building an Ionic project.
I can’t understand the logic of $http in ionic run Android:
I build my application on android, and there isn’t the concept of server side or client side(browser).
If I use $http in order to read a local JSON file, what it will do really?
If I want to create a local json file, it will be better to read it with $Http function or try to read directly the file json ?

Is there a way with angular js or javascript read directly the local file json?

Thanks in adavce


#2

Based on angular docs : https://docs.angularjs.org/api/ng/service/$http

The $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser’s XMLHttpRequest object or via JSONP.

But it allows you also calling local files, because they are going to return the same data that a server would return.

So according to this: http://stackoverflow.com/questions/16930473/angularjs-factory-http-get-json-file
You can try this.

var mainInfo = $http.get('path_to_json_file').success(function(response) {
        return response.data;
    });
}])

Just remember that you have to call the $http dependency wherever you’re calling this piece of code.

I hope this will be helpful for you.