有人可以在 javascript 中解释以下代码吗?它用于读取文本文件,但我不知道 XML 和 AJAX 的东西?
Can someone please explain the following code in javascript; it is used to read a text file, but I don't know about the XML and AJAX stuff?
我正在尝试读取一个文本文件,我的代码完全可以工作。问题是我不理解代码,尤其是所有 onreadystatechange
和 new XMLHttpRequest();
以及 status
的东西——我非常非常困惑!!!!
//load text file, and split each word into an array
function loadwords(myfile){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", myfile, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
//the file is read and stored into a variable
var Text;
Text = rawFile.responseText;
thewords= Text.split("\n");
//The variable Text has a string data type.
//So the string is split into an array.
//Each line being the form of separation between each element
var i;
for (i in thewords){
if ( thewords[i] == "") {
thewords.splice(i,1);
}
}
//The for loop checks for any empty spaces in the array.
//Then removes them from the array using the splice method.
}
}
}
rawFile.send(null);
return thewords;
}
非常感谢!
查看有关 XLMHttpRequest 的文档
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
AJAX代表'Asynchronous Javascript and XML'
尽管这些天我们主要将其用于 json。
当您创建一个新的 XMLHttpRequest 时,您正在设置一个新的 http 请求。 'open' 方法是您添加方法和 url 的地方。打开请求后,您可以通过添加 headers 等开始设置它。最后,您 'send' 请求。这实际上发起了请求。
readyState 卡纸正在说 'when the state of this request changes'。
4恰好是最后一个,当请求完成并收到响应时。状态指的是在 response
中从您刚刚访问的任何服务器发回的 HTTP 状态代码。
你可以在这里查看
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
上面的代码基本上就是说
嘿,我有一个请求。它要去这个地方。如果我收到回复,而且这个回复还不错,那就用它做点什么。
还值得注意的是,当您return底部的单词时,您将得到'undefined'。那是因为它还不存在。 'send' 方法和 'return' 语句之间的时间非常短,我们的请求可能还没有到达服务器。老实说,此时它甚至可能还没有被 发送 。这就是异步编程。要用它做点什么,你应该传递一个回调,并在回调中用它做点什么。您也可以使用承诺。这是一个带有回调的实现。
function loadwords(myfile, callback){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", myfile, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
return callback(rawFile.responseText);
}
}
}
// You can send data here, or pass nothing
return rawFile.send();
}
function handler(text) {
var thewords= text.split("\n");
//The variable Text has a string data type.
//So the string is split into an array.
//Each line being the form of separation between each element
var i;
for (i in thewords){
if ( thewords[i] == "") {
thewords.splice(i,1);
}
}
}
loadWords('someFile', handler);
我正在尝试读取一个文本文件,我的代码完全可以工作。问题是我不理解代码,尤其是所有 onreadystatechange
和 new XMLHttpRequest();
以及 status
的东西——我非常非常困惑!!!!
//load text file, and split each word into an array
function loadwords(myfile){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", myfile, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
//the file is read and stored into a variable
var Text;
Text = rawFile.responseText;
thewords= Text.split("\n");
//The variable Text has a string data type.
//So the string is split into an array.
//Each line being the form of separation between each element
var i;
for (i in thewords){
if ( thewords[i] == "") {
thewords.splice(i,1);
}
}
//The for loop checks for any empty spaces in the array.
//Then removes them from the array using the splice method.
}
}
}
rawFile.send(null);
return thewords;
}
非常感谢!
查看有关 XLMHttpRequest 的文档
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
AJAX代表'Asynchronous Javascript and XML'
尽管这些天我们主要将其用于 json。
当您创建一个新的 XMLHttpRequest 时,您正在设置一个新的 http 请求。 'open' 方法是您添加方法和 url 的地方。打开请求后,您可以通过添加 headers 等开始设置它。最后,您 'send' 请求。这实际上发起了请求。
readyState 卡纸正在说 'when the state of this request changes'。
4恰好是最后一个,当请求完成并收到响应时。状态指的是在 response
中从您刚刚访问的任何服务器发回的 HTTP 状态代码。
你可以在这里查看
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
上面的代码基本上就是说
嘿,我有一个请求。它要去这个地方。如果我收到回复,而且这个回复还不错,那就用它做点什么。
还值得注意的是,当您return底部的单词时,您将得到'undefined'。那是因为它还不存在。 'send' 方法和 'return' 语句之间的时间非常短,我们的请求可能还没有到达服务器。老实说,此时它甚至可能还没有被 发送 。这就是异步编程。要用它做点什么,你应该传递一个回调,并在回调中用它做点什么。您也可以使用承诺。这是一个带有回调的实现。
function loadwords(myfile, callback){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", myfile, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
return callback(rawFile.responseText);
}
}
}
// You can send data here, or pass nothing
return rawFile.send();
}
function handler(text) {
var thewords= text.split("\n");
//The variable Text has a string data type.
//So the string is split into an array.
//Each line being the form of separation between each element
var i;
for (i in thewords){
if ( thewords[i] == "") {
thewords.splice(i,1);
}
}
}
loadWords('someFile', handler);