使用Javascript导入文本文件 [英] Import Text File Using Javascript

查看:149
本文介绍了使用Javascript导入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理处理大量数据的应用程序。目前,我已经将这些值硬编码到Javascript iself(定义全局数组)中,但是这种方法似乎并不可持续。

有没有一种方法可以使用Javascript解析位于服务器上同一目录中的.txt文件?我知道这个问题以前可能已经被问到过了,但我只找到了有关访问系统本地文本文件的答案。

使用AJAX。我建议以JSON格式编码文件,而不是纯文本。如果您使用jQuery,则可以使用 $。getJSON('filename.txt')来读取文件并在一个操作中将其解析为Javascript对象。


I am currently working on an application that handles a fairly large amount of data. Currently, I've hard-coded those values into the Javascript iself (defining global arrays), but this method does not seem sustainable.

Is there a way to use Javascript to parse a .txt file located in the same directory on the server? I know this question has probably been asked before, but I've only found answers pertaining to accessing system-local text files.

解决方案

Use AJAX. I suggest encoding the file in JSON format, rather than plain text. If you use jQuery, you can then use $.getJSON('filename.txt') to read the file and parse it into a Javascript object in one operation.

这篇关于使用Javascript导入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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