简体   繁体   English

没有响应发送时,ajax.responseText的值是什么?

[英]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. 我写了一个JavaScript函数,用于检查用户名在数据库中是否可用。 If the username is NOT available the ajax send a text response back which changes some css and adds an error message. 如果用户名不可用,则ajax发送回文本响应,该响应会更改某些css并添加错误消息。 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. 当用户名可用时,ajax不会发送响应,这很好,但是我只需要知道从ajax.responseText返回什么,因为没有值。 I've tried '' and null. 我试过''和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. 如果服务器脚本退出而未打印任何内容,则响应将为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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