0

我遇到了 ajax responseText 的情况。来自 url 的响应文本功能良好。但是ajax代码里面的东西出了问题。它无法识别响应文本并将类添加到目标 ID。这是代码:

<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);"/>

room_rate_updatez.php 的响应是“成功”和“失败”。我已经尝试了很多次让它工作,但没有运气。请建议。

4

2 回答 2

1
Check the readyState & status like below.

if (xmlHttp.readyState==4 && xmlHttp.status==200)
 {
  var httpResp=xmlhttp.responseText;
 }
var scriptObj1 = $.parseJSON(httpResp);
于 2014-01-16T10:14:43.640 回答
0

尝试这个:

function updateField(nameValue){
   var xmlHttp=null;
   ....
   xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
        if (xmlHttp.status==200){
            //this will be called after update
                var responseText = xmlHttp.responseText;        
                doSomethingAfterUpdate(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 (retValFromPHP == "Failed"){
       document.getElementById("result").innerHTML = "error";
       document.getElementById("result").className = "error" 
    }else{
       document.getElementById("result").innerHTML = "success";
       document.getElementById("result").className = "success" 
    }
}
于 2012-09-13T10:52:25.007 回答