Javascript 如何从ajex返回值作为其父函数的返回输出?

Javascript 如何从ajex返回值作为其父函数的返回输出?,javascript,ajax,web,Javascript,Ajax,Web,我想返回this(1)或this(2)的值作为checkuser的返回值 函数checkuser(){ var x=document.getElementById('SigninWarnig1'); x、 innerHTML=“h”; x、 style.color=“白色”; var username=document.forms['SigninInfo']['username'].value; 如果(username.length==0){ 返回(假); }否则{ 如果(用户名搜索(“”)!=

我想返回
this(1)
this(2)
的值作为
checkuser
的返回值

函数checkuser(){
var x=document.getElementById('SigninWarnig1');
x、 innerHTML=“h”;
x、 style.color=“白色”;
var username=document.forms['SigninInfo']['username'].value;
如果(username.length==0){
返回(假);
}否则{
如果(用户名搜索(“”)!=-1){
x、 innerHTML=“不允许使用空格”;
x、 style.color=“红色”;
返回(假);
}否则{
var-rtn;
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
//console.log(this.responseText);
if(this.responseText==“take”){
x、 innerHTML=“已采取”;
x、 style.color=“红色”;
返回false;//这(1)
}else if(this.responseText==“可用”){
返回true;//这(2)
}
}
};
xhttp.open('POST','。/signup/checkuser.php?用户名='+用户名,true);
xhttp.send();
}
}
}
我在这里已经尝试了一些答案,比如
callback

请帮助我。

例如,您可以定义一个
变量
并在以下步骤后处理它:

function checkuser(){
    var x=document.getElementById('signinwarinig1');

    var res = 0;       // a new variable

    x.innerHTML="h";
    x.style.color="white";
    var username=document.forms['SigninInfo']['username'].value;

    if(username.length == 0){
        res = -1;                        // <-- here would be -1
    }else{
        if(username.search(' ') != -1){
            x.innerHTML="Space Not Allowed";
            x.style.color="red";
            res = -2;                    // <-- here would be -2
        }else{
            var rtn;
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function(){
                if(this.readyState == 4 && this.status==200){
                    // console.log(this.responseText);
                    if(this.responseText == "taken"){
                        x.innerHTML="Already Taken";
                        x.style.color="red";
                        res = 1;                    // <-- here would be 1
                    }else if(this.responseText == "available"){
                        res = 2;                    // <-- here would be 2
                    }
                }
            };
            xhttp.open('POST','./signup/checkuser.php?username='+username, true);
            xhttp.send();
        }
    }
    what_is_res(res);   // or --- return res; // if you need it later
}

function what_is_res(re){
  switch (re) {
     case 0:
        alert( 'default value' );
        break;
     case -1:
        alert( 'first FALSE case' );
        break;
     case -2:
        alert( 'second FALSE case' );
        break;
     case 1:
        alert( 'first REQUEST case' );
        break;
     case 2:
        alert( 'second REQUEST case' );
        break;
     default:
        alert( "non-defined" );
  }
}
函数checkuser(){
var x=document.getElementById('SigninWarnig1');
var res=0;//一个新变量
x、 innerHTML=“h”;
x、 style.color=“白色”;
var username=document.forms['SigninInfo']['username'].value;
如果(username.length==0){

res=-1;//从函数返回一个
Promise

function checkuser(){
    return new Promise((resolve, reject) => {
        var x=document.getElementById('signinwarinig1');
        x.innerHTML="h";
        x.style.color="white";
        var username=document.forms['SigninInfo']['username'].value;

        if(username.length == 0){
            return (false);
        }else{
            if(username.search(' ') != -1){
                x.innerHTML="Space Not Allowed";
                x.style.color="red";
                reject('Space Not allowed'); // this(3)
            }else{
                var rtn;
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function(){
                    if(this.readyState == 4 && this.status==200){
                        // console.log(this.responseText);
                        if(this.responseText == "taken"){
                            x.innerHTML="Already Taken";
                            x.style.color="red";
                            resolve(false); // Resolve the value this(1)
                        }else if(this.responseText == "available"){
                            resolve(true); // Resolve the value this(2)
                        }
                    }
                };
                xhttp.open('POST','./signup/checkuser.php?username='+username, true);
                xhttp.send();
            }
        }
    })
}

checkuser()
    .then(response => {
        console.log(response); // Return true or false from this(1) or this(2)
    })
    .catch(err => {
        console.log(err); // Return "Space Not allowed" from this(3)
    });

这真的是一个“php”问题吗?感谢@Aksen P的快速回复,但这并不令人满意。甚至不接近。请参考我的代码,除了
这(1)
这(2)之外,一切都很好
如果出现这种情况,他们会返回未定义的值。@DipakPatil,真的吗?你在问题中没有说什么,这里一切都很好,除了。因此,你的问题不清楚。添加解释!这个示例返回一个值,正如在这个未解释的问题中所说的。好的,谢谢。我会清楚地提到它。谢谢@Sajeeb Ahamed,你可以帮助我吗e仅返回promisevalue plzzI不明白您想要什么。您在
//处的代码从此(1)或此(2)
控制台上的提示消息返回true或false。我想在进一步的代码中使用结果…间接地,我想在变量中使用结果。