在phonegap中加载存储在文本文件中的数组 [英] Loading an array stored in a text file in phonegap

查看:121
本文介绍了在phonegap中加载存储在文本文件中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有使用jQuery移动或其他框架什么最有效的方式加载二维字符串数据从/ www字典中的文本文件到javascriptgap中的javascript数组?



我试过:

  var request = new XMLHttpRequest()? 

request.open(GET,/android_asset/data.txt);

request.onreadystatechange = function(){//当状态改变时调用函数。
console.log(* data.txt1);

if(request.readyState == 4){

console.log(*+ request.responseText +* data.txt2);

}
};

request.send();

它给我的错误:


[INFO:CONSOLE(0)]XMLHttpRequest无法加载文件:///android_asset/data.txt。跨域源请求只支持HTTP。,source:file:/// android_asset / www / index.html(0)



解决方案

提供了本机文件系统支持。下面的代码应该适用于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){
//使用reader.result数据执行操作
console.log(reader.result);
}
reader.readAsText(f);
},fail);
},fail);
},fail);

fail只是一个用于处理上述代码中的失败情况的回调。


Without using jquery mobile or other framework what the most efficient way to load two dimensional string data from a text file in the /www dictionary into an javascript array in phonegap?

If it matters, feel also free what kind of formatting for the text file is the best.

I tried:

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

It gives me the error:

[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)

解决方案

Since you are using cordova, why not use the native file system support provided. Below code should work for both iOS and 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 is just a callback for handling failure cases in the above code.

这篇关于在phonegap中加载存储在文本文件中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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