2009-06-22 80 views
1

为什么当Ajax调用返回值时,此脚本会导致'undefined'?使用Ajax.Request返回值

function myShippingApp() { 

this.shipper = 0; 

this.init() { 
    this.getShipRate(); 
    alert(this.shipper); 
} 

this.getShipRate = function() { 

    var zip = $('zip').value; 
    if(zip == '') { 
     return false; 
    } else { 
     var url = 'getrate.php?zip='+zip; 
     this.shipper = new Ajax.Request(url, { 
      onComplete: function(t) { 
       $('rates').update("$"+t.responseText); 
       return t.responseText; 
      } 
     }); 
    } 
} 

}

我与原型框架内工作,并且有麻烦返回值回对象。 我在做什么错?

谢谢!

+2

重复http://stackoverflow.com/questions/1005942/get-ajax-response/1005987 – Surya 2009-06-22 21:31:10

回答

0

Ajax.Request不返回任何值,它是一个对象的实例化。

我想你可以说,价值是对象本身。

2

你想要的值是在t.responseText中,它不会被Ajax.Request对象'返回',因此this.shipper永远不会被赋值。

这可能是沿着你想要的线条更:

function myShippingApp() { 

    this.shipper = 0; 

    this.init() { 
    this.getShipRate(); 
    } 

    this.getShipRate = function() { 
    var zip = $('zip').value; 
    if(zip == '') { 
     return false; 
    } else { 
     var url = 'getrate.php?zip='+zip; 
     new Ajax.Request(url, { 
     onComplete: function(t) { 
      $('rates').update("$"+t.responseText); 
      this.shipper = t.responseText; 
      alert(this.shipper); 
     } 
     }); 
    } 
    } 
} 

让我知道它是否适合你。