+ -
当前位置:首页 → 问答吧 → 请教一下一个JS脚本的问题

请教一下一个JS脚本的问题

时间:2011-09-03

来源:互联网

我前段时间跟着教程学了个AJAX的JS脚本,下面是代码:

JScript code

var xmlHttp;
function S_xmlhttprequest()
{
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
    } else if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
                            
}

function postandsend(username){
    var f=document.form1.username.value;
    S_xmlhttprequest();
    xmlHttp.open("GET","namechk.php?name="+f,true);
    xmlHttp.onreadystatechange = sendvar;
    xmlHttp.send(null);
}



function sendvar(){

    if(xmlHttp.readyState == 1){
        document.getElementById('chkname').innerHTML = "Loading..."
    }
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
                var getvar = xmlHttp.responseText;
                    document.getElementById('chkname').innerHTML = getvar;
        }
    }    
    
function refresh1(){
    S_xmlhttprequest();
    xmlHttp.open("GET","v_code.php",true);
    xmlHttp.onreadystatechange = getnewcode;
    xmlHttp.send(null);
}



function getnewcode(){

    if(xmlHttp.readyState == 1){
        document.getElementById('v_codeimg').innerHTML = "Loading..."
    }
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
                var getvar = xmlHttp.responseText;
                    document.getElementById('v_codeimg').innerHTML = getvar;
        }
    }        

}}    
    
/*
function refresh1(){
    S_xmlhttprequest();
    xmlHttp.open("GET","v_code.php",true);
    xmlHttp.onreadystatechange = getnewcode;
    xmlHttp.send(null);
}    
    
function getnewcode(){

    if(xmlHttp.readyState == 1){
        document.getElementById('v_codeimg').innerHTML = "Loading..."
    }
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
                var getvar = xmlHttp.responseText;
                    document.getElementById('v_codeimg').innerHTML = getvar;
        }
    }    */






前面三个函数用于判断用户名是否存在的,都没有问题(这个是按照教程做的) 可是我后来又试图用这个办法实现验证码的“看不清,换一张”的功能,通过AJAX和验证码PHP图片交互,于是我根据教程,改写出后面二个函数,结果却提示我“缺少对象”,我检查了数次代码,却没能发现问题的所在。 而IE提示的错误行数却根本对不上。


另:请问有没有什么办法调试JS的代码,至少可以正确的至少错误出在那一行呢?

作者: kkkgho   发布时间: 2011-09-03

晕,重新写了下,问题解决了,但是还是没搞明白之前的写法为什么不行,虽然不规范。

哪位大大可否帮我解答一下,以下是解决以后的JS代码

JScript code

var xmlHttp;
function S_xmlhttprequest()
{
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
    } else if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
                            
}

function postandsend(username,is_code){
    var f=document.form1.username.value;
    S_xmlhttprequest();
    if(is_code=="1")
    {
        xmlHttp.open("GET","v_code_htm.php?name="+f,true);
        xmlHttp.onreadystatechange = sendvar;
        xmlHttp.send(null);        
    }
    else
    {
        xmlHttp.open("GET","namechk.php?name="+f,true);
        xmlHttp.onreadystatechange = sendvar2;
        xmlHttp.send(null);                
    }
}



function sendvar(){

    if(xmlHttp.readyState == 1){
        document.getElementById('v_codeimg').innerHTML = "Loading..."
    }
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
                var getvar = xmlHttp.responseText;
                    document.getElementById('v_codeimg').innerHTML = getvar;
        }
    }}    
    
function sendvar2(){

    if(xmlHttp.readyState == 1){
        document.getElementById('chkname').innerHTML = "Loading..."
    }
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
                var getvar = xmlHttp.responseText;
                    document.getElementById('chkname').innerHTML = getvar;
        }
    }}        
    
function refresh1(){
    S_xmlhttprequest();
    xmlHttp.open("GET","v_code_htm.php",true);
    xmlHttp.onreadystatechange = getnewcode;
    xmlHttp.send(null);
}



function getnewcode(){

    if(xmlHttp.readyState == 1){
        document.getElementById('v_codeimg').innerHTML = "Loading..."
    }
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
                var getvar = xmlHttp.responseText;
                    document.getElementById('v_codeimg').innerHTML = getvar;
        }
    }        

}




作者: kkkgho   发布时间: 2011-09-03

firebug

作者: Sencha_Android   发布时间: 2011-09-03

ajax 请求 都要加 一个时间戳!

不然不会重新请求服务器! (浏览器缓存)

作者: hch126163   发布时间: 2011-09-03