0

我编写了一个 javascript 函数来检查数据库中是否有可用的用户名。如果用户名不可用,则 ajax 会返回一个文本响应,该响应会更改一些 css 并添加一条错误消息。当用户名可用时,ajax 不会发送响应,这很好,但我只需要知道从 ajax.responseText 返回的内容,因为没有任何价值。我试过''和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);
    }
 }
}
4

1 回答 1

1

除非请求超时,否则总会有响应。如果服务器脚本退出而没有打印任何内容,则响应将是一个空字符串。

于 2013-06-18T00:56:50.123 回答