0

For my Phonegap App I'm trying to load a JSON file using angulars $http.

I got this service:

cApp.factory('language', function ($http) {
    return {
        getLanguageData: function () {
            return $http.get('../../lang/' + lang.substr(0,2) + '.json');
        }
    }
});

Which I use in this controller:

cApp.controller('initController', function ($scope, language) {   
    language.getLanguageData().success(function (data) {        
        $scope.language = data;
    }).catch(function (e) {
        alert("error" + e);
    });
});

This works fine in my browser, but not in Phonegap Developerapp on Android. The controller does not write the language variable, and does not alert anything (not even "error").

What i tried:

With a .catch().then().catch() chain it returned me data which was null in the last .catch().

I tought about if it's a cross origin problem, but my phonegap whitelist allows all domains (<access origin="*" />).

The files should be in the same domain anyways, since my folder structure looks like this:

.myapp
├── www
|   ├── js
|   |   ├── controller
|   |   |   └── initController.js
|   |   └── service
|   |       └── language.js
|   └── lang
|       ├── en.json
|       └── de.json
└── config.xml

What am I doing wrong, missing?

UPDATE: Partial solution found:

It works if i execute the get() using the whole path:

return $http.get('file:///data/data/com.adobe.phonegap.app/files/phonegapdevapp/www/json/lang/' + lang.substr(0,2) + '.json');

That's not a proper solution since it won't work on iOS (different path). I don't get why I can't use ../../lang/.

2
  • I think you probably have the wrong relative path. Maybe you are going too many levels up and the browser version hits the root folder and ignores the .. but the phonegap app goes up too far. Probably you should be using '/lang/' as the path. Commented Jan 6, 2016 at 14:09
  • I tried it but it does not do the trick. My controller JS is in this folder on the app: ´file:///data/data/com.adobe.phonegap.app/files/phonegapdevapp/www/js/controller/´ so I think the path might be fine Commented Jan 6, 2016 at 14:19

1 Answer 1

0

You should just handle it as file using the file-api for cordova apps.

Sign up to request clarification or add additional context in comments.

Comments

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.