简体   繁体   中英

What is the value of ajax.responseText when there is no response being sent?

I wrote a javascript function that checks if a username is available in a database. If the username is NOT available the ajax send a text response back which changes some css and adds an error message. When the username is available the ajax doesn't send a response, which is fine but I just need to know what is being returned from ajax.responseText since there is no value. I've tried '' and null.

function _(x) {
return document.getElementById(x);
}

function ajaxObj(meth, url) {
var x = new XMLHttpRequest(); 
x.open( meth, url, true );  
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
return x;
}
function ajaxReturn(x) {
if(x.readyState == 4 && x.status == 200) {
    return true;
    }
}
function verifyEmail(){

var email = _("email").value;
var status = _("estatus");     
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;


if (document.signupform.email.value.search(emailRegEx) == -1) {
    _("emaildiv").style.border = "1px solid #d33e3e";
    _("emaildiv").style.backgroundColor = "#fadede";
    status.innerHTML = "<br />Please enter a valid email address";
 } else {
    _("emaildiv").style.border = "";
    _("emaildiv").style.backgroundColor = "";
    status.innerHTML = "";

    if (email != "") {
        status.innerHTML = "checking. . . "; 
        var ajax = ajaxObj("POST", "fan_signup_local.php");    
        ajax.onreadystatechange = function() {
            if (ajaxReturn(ajax) == true) {
                status.innerHTML = ajax.responseText;
                console.log(status.innerHTML);
                if (status.innerHTML !== '') {
                    _("emaildiv").style.border = "1px solid #d33e3e";
                    _("emaildiv").style.backgroundColor = "#fadede";
                    console.log(ajax.responseText);
                } else {
                    _("emaildiv").style.border = "";
                    _("emaildiv").style.backgroundColor = "";
                }
            }
        }
        ajax.send("email="+email);
    }
 }
}

There's always a response, unless the request times out. If the server script exits without printing anything, the response will be an empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM