Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

javascript - xmlhttp.responseText not display text

I'm having a situation with ajax responseText. The response text from url is well function. But something inside the ajax code goes wrong. It doesn't recognize the response text and add class to the targeted id. Here's the code :

<script type="text/javascript">
function updateField(nameValue){
    var xmlHttp=null;
    try{
        xmlHttp=new XMLHttpRequest();
        }
catch (e){
    try{
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
catch (e){
    alert("No AJAX!");
    return false;
    }
}
xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
        if (xmlHttp.status==200){
            //this will be called after update
                var responseText = xmlHttp.responseText;        
            }
        }
    }
    //this will send the data to server to be updated
    xmlHttp.open("GET", 'inc/room_rate_updatez.php?'+ nameValue, true);
    xmlHttp.send(null);
}

function doSomethingAfterUpdate(retValFromPHP){
//retValFromPHP can be any thing you want!

    if (reponseText == "Failed"){
       document.getElementById("result").innerHTML=xmlhttp.responseText.className = "error";
    }else{
       document.getElementById("result").innerHTML=xmlhttp.responseText.className = "success";
    }
}

</script>

<div id="result"></div><input type="text" name="rate|498|6500-5200-4600-5600-4100|0" id="498" value="6500" size="10" onchange="updateField(this.name + '=' + this.value);"/>

The response from room_rate_updatez.php are "Succeed" and "Failed". I've tried many times to make it work but no luck. Please suggest.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Check the readyState & status like below.

if (xmlHttp.readyState==4 && xmlHttp.status==200)
 {
  var httpResp=xmlhttp.responseText;
 }
var scriptObj1 = $.parseJSON(httpResp);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...