我有一个使用 ionic 框架的 Cordova 应用程序。我有很多 json 数据文件,我将它们放在应用程序文件树中的 www/json 文件夹中。我正在使用 angularJS http 调用来访问它们。
当我在 chrome 中测试我的应用程序时(在终端中使用“ionic serve”)它工作正常但是当我在 android 设备(nexus 5 和 Android 6.0 Marshmallow)上测试它时它不再工作。
示例代码
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...
});
}
我试过添加
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...
});
}
当我在手机上调试应用程序时,出现以下错误。
GET file:///android_asset/bookfolder/Books.json net::ERR_FILE_NOT_FOUND
如果有人知道我做错了什么或知道访问本地 .json 文件的更好方法,请告诉我。谢谢!
最佳答案
使用斜杠 /
字符,而不是反斜杠 \
。另外,将 bookfolder
放入 /android_asset/www
$http
.get('/android_asset/www/bookfolder/books.json')
.then(function(response){ return response.data });
关于android - 如何使 ionic 应用程序从应用程序本地的 .json 文件中读取 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34110235/