在不使用jquery mobile或其他框架的情况下,将二维字符串数据从/www字典中的文本文件加载到phonegap中的javascript数组中的最有效方法是什么?
如果重要的话,也请随意选择哪种文本文件格式是最好的。
我尝试过:
var request = new XMLHttpRequest();
request.open("GET", "/android_asset/data.txt");
request.onreadystatechange = function() {//Call a function when the state changes.
console.log("* data.txt1 " );
if (request.readyState == 4) {
console.log("*" + request.responseText + "* data.txt2" );
}
};
request.send();
它给了我错误:
[INFO:CONSOLE(0)] "XMLHttpRequest cannot load file:///android_asset/data.txt. Cross origin requests are only supported for HTTP.", source: file:///android_asset/www/index.html (0)
最佳答案
既然你使用cordova,为什么不使用提供的 native 文件系统支持呢。下面的代码应该适用于 iOS 和 Android。
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
fs.root.getFile("../myapp/www/content/" + "words.txt", null, function(fe)
{
fe.file(function(f) {
var reader = new FileReader();
reader.onloadend = function(evt) {
// do sth with the reader.result data
console.log(reader.result);
}
reader.readAsText(f);
}, fail);
}, fail);
}, fail);
fail 只是上面代码中处理失败情况的回调。
关于javascript - 加载存储在phonegap中的文本文件中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25382572/