Angular $http.get() does not work in Phonegap DevA

2019-09-14 00:23发布

问题:

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/.

回答1:

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