代码之家  ›  专栏  ›  技术社区  ›  Dan Appleyard

jquery-ajax计时有问题

  •  0
  • Dan Appleyard  · 技术社区  · 15 年前

    我有一个非常简单的javascript类,它通过jquery对我的Web服务进行Ajax调用。它成功地返回数据,但我无法通过我设置数据的变量来检索它。我不认为Ajax调用是异步的或者不是异步的,因为我已经为所有Ajax事件设置了事件处理程序,但其中一些事件没有触发。我不知道怎么了。以下是完整的代码:

    Javascript:

    function testClass(){
        this.returnData = "";
        this.FireAjax = function(){
            $.getJSON("http://localhost/mywebapp/webservices/service.asmx/Initialize?userID=12&jsoncallback=?",
                function(data){
                    this.returnData = data.d;
                    alert(data.d);
                }
            );  
        }
    
    }
    

    HTML页面:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript" src="http://localhost/mywebapp/js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="testClass.js"></script>
    
    <script type="text/javascript">
        $(document).ready(function(){
    
            var obj = new testClass();
    
            $("#debug").ajaxError(function(event, request, settings){
                $(this).append("<b>Ajax Error!</b><br />"); //this does not fire
            });
    
            $("#debug").ajaxSend(function(evt, request, settings){
                $(this).append("<b>Ajax Send!</b><br />"); //this does not fire!?
            });
    
            $("#debug").ajaxStop(function(){
                $(this).append("<b>Ajax Stopped</b><br />"); //this fires
            });
    
            $("#debug").ajaxComplete(function(event,request, settings){
                $(this).append("<b>Ajax Completed!</b><br />"); //this fires
                $(this).append("<h2>" + obj.returnData + "</h2>"); //this returns an empty string!!!!!!
            });
    
            $("#debug").ajaxSuccess(function(evt, request, settings){
                $(this).append("<b>Ajax Successful!</b><br />"); //this fires
            });
    
            $("#debug").ajaxStart(function(){
                $(this).append("<b>Ajax Started!</b><br />"); //this fires
            });
    
            obj.FireAjax();
        });
    </script>
    </head>
    
    <body>
    <div id="debug">
    
    </div>
    </body>
    </html>
    

    其他信息: 如果我删除HTML页面中的完整事件,并将对obj.returnData的调用放在stop事件中(认为可能我的HTML完整事件会覆盖我的testclass complete函数),我会得到相同的结果。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sam Hasler zpesk    15 年前

    你的问题在这里:

    this.returnData = data.d;
    

    this 匿名函数内部引用的是jquery选项对象,而不是对象的实例。

    试试这个:

    function testClass(){
        this.returnData = "";
        var that = this;
        this.FireAjax = function(){
            $.getJSON("http://localhost/mywebapp/webservices/service.asmx/Initialize?userID=12&jsoncallback=?",
                    function(data){
                            that.returnData = data.d;
                            alert(data.d);
                    }
            );      
        }
    
    }