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

北战南征 提交于 2019-12-12 03:33:54

问题


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.



来源:https://stackoverflow.com/questions/34634992/angular-http-get-does-not-work-in-phonegap-devapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!