1

I have a Cordova app that uses the ionic framework. I have many json data files that I put in the www/json folder in my app's file tree. I am using angularJS http calls to access them.

When I test my app in chrome (using "ionic serve" in the terminal) it works fine but when I test it on an android device(nexus 5 with Android 6.0 Marshmallow) it no longer works.

Sample code

function getBookNames() {
      return $http.get("..\\bookfolder\\Books.json")
        .then(function (data) {//if success than do
          return data.data;
        }, function (reason) {// if fail than do
          // maybe tell the user what happened...
        });
    }

I have tried adding

 var path = "";

    if (ionic.Platform.isAndroid()) {
      path = "\\android_asset\\www\\";
    }
    function getBookNames() {
          return $http.get(path + "..\\bookfolder\\Books.json")
            .then(function (data) {//if success than do
              return data.data;
            }, function (reason) {// if fail than do
              // maybe tell the user what happened...
            });
        }

when I am debugging the app on my phone I get the following error.

GET file:///android_asset/bookfolder/Books.json net::ERR_FILE_NOT_FOUND

If anyone knows what I am doing wrong or of a better way to access local .json files please let me know. Thanks!

1 Answer 1

1

Use slash / character, not backslash \. Also, put bookfolder inside /android_asset/www

$http
    .get('/android_asset/www/bookfolder/books.json')
    .then(function(response){ return response.data });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Pete! This was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.