角$ http.get()在Phonegap DevApp中不起作用 [英] Angular $http.get() does not work in Phonegap DevApp

查看:103
本文介绍了角$ http.get()在Phonegap DevApp中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的Phonegap应用,我尝试使用角度$ http加载JSON文件.

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

我得到了这项服务:

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);
    });
});

这在我的浏览器中可以正常工作,但在Android上的Phonegap Developerapp中却不能.控制器不写入language变量,也不警告任何内容(甚至不包括错误").

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").

我尝试过的事情:

使用.catch().then().catch()链返回了最后一个.catch()中的null数据.

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

我想知道这是否是跨源问题,但是我的phonegap白名单允许所有域(<access origin="*" />).

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?

更新:找到了部分解决方案:

如果我使用整个路径执行get(),它将起作用:

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');

这不是一个合适的解决方案,因为它无法在iOS(不同的路径)上运行.我不明白为什么我不能使用../../lang/.

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

推荐答案

您应该使用

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

这篇关于角$ http.get()在Phonegap DevApp中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆