javascript - 加载存储在phonegap中的文本文件中的数组

标签 javascript cordova

在不使用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/

相关文章:

javascript - 无法设置 null 的属性 'innerHTML'?

javascript - 在搜索栏中显示列表中的多个项目

javascript - Vee Validate 3.x,定义ValidationFlags 类型

javascript - CodeMirror 没有完全向下滚动,Cursor 隐藏在 div 后面

ios - 应用程序崩溃并显示 <gap :config-file platform ="ios" parent ="NSLocationAlwaysUsageDescription" overwrite ="false"> in config. xml

javascript - Axios 请求在 cordova 应用程序的签名版本上失败

android - Cordova 的地理定位插件如何工作?

javascript - 向递归函数添加类型

Cordova 3.0 : Android: Connection is not defined

macos - 环境 : node\r: No such file or directory with cordova cli