将字符串拆分为数组时出现JavaScript错误

将字符串拆分为数组时出现JavaScript错误,javascript,html,arrays,string,file,Javascript,Html,Arrays,String,File,我遇到了将字符串拆分为数组的问题。为了帮助自己解决问题,我包括了两个警报函数,但只调用了一个。因此,我知道将字符串拆分为数组进行基本用户名/密码检查时会出现问题。以下是我的JS代码: function check() { var user = document.loginform.usr.value; var pass = document.loginform.psw.value; var valid = false; var txt = new XMLHttpRequest(); var a

我遇到了将字符串拆分为数组的问题。为了帮助自己解决问题,我包括了两个警报函数,但只调用了一个。因此,我知道将字符串拆分为数组进行基本用户名/密码检查时会出现问题。以下是我的JS代码:

function check() {
var user = document.loginform.usr.value;
var pass = document.loginform.psw.value;
var valid = false;

var txt = new XMLHttpRequest();
var alltext = "";
var allLines = [];
var usrn = [];
var pswd = [];

txt.open("GET", "/c.txt", true);

alltext = txt.responseText;
allLines = alltext.split(/\r\n|\n/);

usrn = allLines[0].split(',');
alert("usrn split");
pswd = allLines[1].split(',');
alert("pswd split");

for (var i=0; i <usrn.length; i++) {
    if ((user == usrn[i]) && (pass == pswd[i])) {
        valid = true;
        break;
    }
}

if(valid) {
    window.location = "test.html";
    return false;
}else{
    var div = document.getElementById("login");
    div.innerHTML = '<font color="red" size=2><i>Invalid Username/Password!</i></font><br>' + div.innerHTML;
}
当User1在表单中输入他/她的名字时,密码应该是pass。但是,脚本在pswd=allLines[1]处停止。拆分',';。我是不是误解了线阵列


感谢您的帮助-谢谢

您需要通过将线路更改为

txt.open("GET", "/c.txt", false);
或者使用onreadystatechange事件在服务器返回响应时获取响应

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    alltext = txt.responseText;
    allLines = alltext.split(/\r\n|\n/);

    usrn = allLines[0].split(',');
    alert("usrn split");
    pswd = allLines[1].split(',');
    alert("pswd split");

    for (var i=0; i <usrn.length; i++) {
        if ((user == usrn[i]) && (pass == pswd[i])) {
            valid = true;
            break;
        }
    }

    if(valid) {
        window.location = "test.html";
        return false;
    }else{
        var div = document.getElementById("login");
        div.innerHTML = '<font color="red" size=2><i>Invalid Username/Password!</i></font><br>' + div.innerHTML;
    }
    }
  }
您需要调用txt.send。而且它是异步的,所以txt.responseText很可能为空

您可以像这样使用onreadystatechanged来确保txt.responseText有一个值:

txt.onreadystatechange = function() {
    if (txt.readyState == 4) { // 4 = DONE
        alert(txt.responseText);
    }
}

好的-在修改了代码并做了更多的研究之后,我得到了一个工作脚本。此脚本从表单中获取数据,并将其与文件c.txt进行检查。如果表单条目与c.txt中的用户名/密码组合匹配,则会将您带到另一个网页

function check() {
var user = document.loginform.usr.value;
var pass = document.loginform.psw.value;
var valid = false;

var txt;

if(window.XMLHttpRequest){
     txt = new XMLHttpRequest();
}else{
    txt = new ActiveXObject("Microsoft.XMLHTTP");
}

var allLines = [];
var usrn = [];
var pswd = [];

txt.onreadystatechange=function() {
    if(txt.readyState==4 && txt.status==200){
        var alltext = txt.responseText;
        allLines = alltext.split(/\r\n|\n/);

        usrn = allLines[0].split(',');
        pswd = allLines[1].split(',');

        for (var i=0; i <usrn.length; i++) {
            if ((user == usrn[i]) && (pass == pswd[i])) {
                valid = true;
                break;
            }
        }

        if(valid) {
            window.location = "test.html";
            return false;
        }else{
            var div = document.getElementById("login");
            div.innerHTML = '<font color="red" size=2><i>Invalid Username/Password!</i></font><br>' + div.innerHTML;
        }
    }
}

txt.open("GET", "c.txt", false);
txt.send();

}

异步Javascript和Xml。@Teemu你是什么意思?异步线程真的会干扰字符串拆分吗?不会,但在您尝试使用它时,txt.responseText没有定义。@Teemu-oooh好的,我明白了。我漏了一行?那可能是什么?下面有一些有效的解决方案,请选择你的。嗯。。。我不确定这是否解决了我的问题。我主要是想把重点放在pswd=allLines[1]上;线这就是我认为一切都出了问题的地方。当我运行脚本时,确实会收到usrn拆分警报,但从来不会收到pswd拆分警报。我猜没有allLines[1]条目,因为如果我将其更改为0,则会显示pswd拆分消息。是我把它拆分错了,还是readyState行真的那么重要?如果您使用console.logalltext,它看起来像什么?
function check() {
var user = document.loginform.usr.value;
var pass = document.loginform.psw.value;
var valid = false;

var txt;

if(window.XMLHttpRequest){
     txt = new XMLHttpRequest();
}else{
    txt = new ActiveXObject("Microsoft.XMLHTTP");
}

var allLines = [];
var usrn = [];
var pswd = [];

txt.onreadystatechange=function() {
    if(txt.readyState==4 && txt.status==200){
        var alltext = txt.responseText;
        allLines = alltext.split(/\r\n|\n/);

        usrn = allLines[0].split(',');
        pswd = allLines[1].split(',');

        for (var i=0; i <usrn.length; i++) {
            if ((user == usrn[i]) && (pass == pswd[i])) {
                valid = true;
                break;
            }
        }

        if(valid) {
            window.location = "test.html";
            return false;
        }else{
            var div = document.getElementById("login");
            div.innerHTML = '<font color="red" size=2><i>Invalid Username/Password!</i></font><br>' + div.innerHTML;
        }
    }
}

txt.open("GET", "c.txt", false);
txt.send();