Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将两个代码合并为一个-检查url是否存在/登录_Javascript - Fatal编程技术网

Javascript 将两个代码合并为一个-检查url是否存在/登录

Javascript 将两个代码合并为一个-检查url是否存在/登录,javascript,Javascript,我已经编写了一个代码来根据用户输入(令牌)重新链接。 例如:如果用户插入1234,然后按enter键或按login按钮。该网站将重定向至:example.com/login/1234 现在我想将它与url检查结合起来。因此,如果url不存在,他们会收到一条消息,表明令牌无效,而不是404错误 这是登录的有效代码: var input = document.getElementById("token"); input.addEventListener("keyup", function(event

我已经编写了一个代码来根据用户输入(令牌)重新链接。 例如:如果用户插入1234,然后按enter键或按login按钮。该网站将重定向至:example.com/login/1234

现在我想将它与url检查结合起来。因此,如果url不存在,他们会收到一条消息,表明令牌无效,而不是404错误

这是登录的有效代码:

var input = document.getElementById("token");
input.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("loginenter").click();
    }
});

function changeQuery(){
    var input_query = document.getElementById('token').value;
    window.parent.location = "https://www.example.com/login/" + input_query;
}
这是对无效的断开URL的检查:

var request = new XMLHttpRequest();
var status;
var statusText;
request.open("GET", URL, true);
request.send();
request.onload = function(){
    status = request.status;
    statusText = request.statusText;
}

if (status == 200) {
    //if(statusText == OK)
    window.parent.location = "https://www.example.com/login/" + input_query;
} else {
   console.log("not valid");
}
我有以下几点:

var input = document.getElementById("token");

var request = new XMLHttpRequest();
var status;
var statusText;
request.open("GET", URL, true);
request.send();
request.onload = function(){
    status = request.status;
    statusText = request.statusText;
}
if(status == 200) {
    //if(statusText == OK)
    window.parent.location = "https://www.example.com/login/" + input_query;
} else {
   console.log("not valid");
}

input.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("loginenter").click();
    }
});

function changeQuery(){
    var input_query = document.getElementById('token').value;

    var request = new XMLHttpRequest();
    var status;
    var statusText;
    request.open("GET", URL, true);
    request.send();
    request.onload = function(){
        status = request.status;
        statusText = request.statusText;
    }
    if(status == 200) {
        //if(statusText == OK)
        window.parent.location = "https://www.example.com/login/" +      
input_query;
    } else {
        console.log("not valid");
    }

我希望它返回一条消息“令牌无效”,但它似乎不起作用。

您正在使用
request.onload
作为回调,但从外部检查状态。尝试在
onload
函数中检查您的状态。您的请求代码似乎重复了。调用
changeQuery
函数的时间/位置?您使用
request.onload
作为回调,但从外部检查状态。尝试在
onload
函数中检查您的状态。您的请求代码似乎重复了。
changeQuery
函数何时/何处调用?