xmlhttp.responseText不显示文本 [英] xmlhttp.responseText not display text

查看:293
本文介绍了xmlhttp.responseText不显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了ajax responseText的情况.来自url的响应文本功能良好.但是,ajax代码内部出现错误.它无法识别响应文本,并且将类添加到目标ID.这是代码:

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

room_rate_updatez.php的响应为成功"和失败". 我已经尝试过很多次了,但是没有运气.请提出建议.

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.

推荐答案

尝试一下:

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" 
    }
}

这篇关于xmlhttp.responseText不显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆