我正在使用jquery mobile创建一个web应用程序... 我想知道什么是最好的方式来阅读文本文件? 在这一刻,我有这个功能正常工作......但我不知道他们之间的差异,哪一个更好用?或者是最快的...webapp读取文本文件ajax,httprequest,或
功能1
function readfileAjax() {
$.get('txt/info.txt', function(txt) {
var lines = txt.split(/\n/);
var randLineNum = Math.floor(Math.random() * lines.length);
var text = lines[randLineNum];
var parts = text.split(/#/);
var fullText = parts[0] + " " + parts[1] + " " + parts[2];
$("#msg").append("<p>" + fullText + "</p>");
});
}
功能2
function readfileHttpRequest() {
var filePath = "txt/info.txt";
xmlhttp = new XMLHttpRequest();
//xmlhttp.overrideMimeType('text/plain');
xmlhttp.open("GET",filePath,false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
var lines = fileContent.split(/\n/);
var randLineNum = Math.floor(Math.random() * lines.length);
var text = lines[randLineNum];
var parts = text.split(/#/);
var fullText = parts[0] + " " + parts[1] + " " + parts[2];
$("#msg").append("<p>" + fullText + "</p>");
}
感谢
了解您正在使用的功能。 http://api.jquery.com/jquery.get/ – epascarello 2014-12-02 03:04:14