本帖最后由 generalxu 于 2010-06-10 15:24:20 编辑

解决方案 »

  1.   

    可能是ajax异步请求的原因,还没等返回呢就接着执行下面的代码了
      

  2.   

     s.ajaxrequest();
     alert(s.response)
    你用下状态判断,你ff看下它执行了几次
      

  3.   

    js代码是单线程执行,你发送ajax后,有可能还没等到tmp.status==200呢,就执行了alert(s.response).
    应该绑定个回调事件。<script type='text/javascript'>  var ajax=function (){};
    ajax.prototype.xmlhttp=null;
    ajax.prototype.url='';
    //ajax.prototype.response=null;
    ajax.prototype.seturl=function(url)
    {
        this.url=url;
    }
    ajax.prototype.setaction=function(action)
    {
        this.action=action;
    }
    ajax.prototype.setstr=function(str)
    {
        this.str=str;    
    }
    ajax.prototype.callBack = {
    success : function(o){}
       ,fail    : function(o){} 
    }
    ajax.prototype.ajaxrequest=function()
    {
            if(window.ActiveXObject)
            {
                this.xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');    
            }
            else if(window.XMLHttpRequest)
            {
                this.xmlhttp=new XMLHttpRequest();    
            }
            
            if(this.xmlhttp!=null)
            {
                var tmp = this.xmlhttp;
                var self  = this;
                this.xmlhttp.onreadystatechange=function ()
                {
                    if(tmp.readyState==4)
                    {
                        if(tmp.status==200)
                        {
                           // ajax.prototype.response=tmp.responseText;
                            self.callBack.success.apply(tmp,[tmp.responseText])
                        }
                        else
                        {
                         self.callBack.success.apply(tmp,[tmp.statusText])
                        }
                    }
                }
                
                if(this.action=='get')
                {
                    this.xmlhttp.open('GET',this.url,true);
                    this.xmlhttp.send(null);
                }
                else if(this.action=='post')
                {
                    this.xmlhttp.open('POST',this.url,true);
                    this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
                    this.xmlhttp.send(this.str);
                }
            }
            else
            {
                alert('Your browser does not support AJAX');    
            }
    }
    function click1()
    {
    alert(1);
        var s= new ajax;
        s.seturl('s1.php?'+Math.random());
       // s.setaction('alert');
        s.setstr(null);
        s.setaction('get');
        s.ajaxrequest();
        s.callBack = {
         success : function(text){
         alert(text);
         }
           ,fail    : function(text){
            alert("Error:"+text);
           }
        }
        s=null;
    }
        </script>
      

  4.   

    <script type='text/javascript'>     var ajax=function (){};
        ajax.prototype.xmlhttp=null;
        ajax.prototype.url='';
        //ajax.prototype.response=null;
        ajax.prototype.seturl=function(url)
        {
            this.url=url;
        }
        ajax.prototype.setaction=function(action)
        {
            this.action=action;
        }
        ajax.prototype.setstr=function(str)
        {
            this.str=str;    
        }
        ajax.prototype.callBack = {
            success : function(o){}
           ,fail    : function(o){} 
        }
        ajax.prototype.ajaxrequest=function()
        {
                if(window.ActiveXObject)
                {
                    this.xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');    
                }
                else if(window.XMLHttpRequest)
                {
                    this.xmlhttp=new XMLHttpRequest();    
                }
                
                if(this.xmlhttp!=null)
                {
                    var tmp        = this.xmlhttp;
                    var self     = this;
                    this.xmlhttp.onreadystatechange=function ()
                    {
                        if(tmp.readyState==4)
                        {
                            if(tmp.status==200)
                            {
                               // ajax.prototype.response=tmp.responseText;
                                self.callBack.success.apply(tmp,[tmp.responseText])
                            }
                            else
                            {
                                self.callBack.fail.apply(tmp,[tmp.statusText])
                            }
                        }
                    }
                    
                    if(this.action=='get')
                    {
                        this.xmlhttp.open('GET',this.url,true);
                        this.xmlhttp.send(null);
                    }
                    else if(this.action=='post')
                    {
                        this.xmlhttp.open('POST',this.url,true);
                        this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
                        this.xmlhttp.send(this.str);
                    }
                }
                else
                {
                    alert('Your browser does not support AJAX');    
                }
        }
        function click1()
        {
            var s= new ajax;
            s.seturl('s1.php?'+Math.random());
           // s.setaction('alert');
            s.setstr(null);
            s.setaction('get');
            s.ajaxrequest();
            s.callBack = {
                success : function(text){
                    alert(text);
                }
               ,fail    : function(text){
                       alert("Error:"+text);
               }
            }
            s=null;
        }
           </script>