Hi, my ionic application needs to read json data from local file. I’m using $http.get method and works just fine in 127.0.0.1 (as web app – using ionic start), but when running on actual ANDROID device, it returns 404.
$http.get('js/data/someData.json').success(function(response){ //do something with response });
Seems like the path is different for android. I did some search on google, but couldn’t find a good solution. Any suggestions?
what ‘search key’ i have to use?
i didn’t find one of it… Sorry
if i’m going to use this code “$http”, what exactly i have to do first?
or just write the code in controller/module?
or maybe anyelse i have to do if im going to use the code?
I don’t seem to have any issues with loading a JSON file from an Android device or from the Ionic View app on Android. Here’s the code I use at the top of a controller. My JSON file is in an appdata folder directly under the www folder. For example, the file is in www/appdata/file.json.
Give this a try and see if it works for you.
.controller('loadController', function ($scope, $http) {
$http.get('appdata/file.json')
.success(function (data) {
// The json data will now be in scope.
$scope.myJsonData = data;
});
});
var url = "";
if(ionic.Platform.isAndroid()){
url = "/android_asset/www/";
}
then use $http
$http
.get(url+'js/data/filename.json')
.success(function(response){ // do something });
so for iOS, url should be just pointing from /www/ folder…
in my case i had /www/js/data/filename.json,
so for iOS, i used /js/data/filename.json directly
Thanks a lot @ozexpert… Just a bit curious, why did we go for $http.get() service for fetching a json stored locally? Is there another way we can get the json (I tried using angular.fromJson() but it requires to have a ready json object, we can’t pass in a file path)? Pardon me, just starting with ionic and angular.