2009-06-22 29 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); 
     } 
     }); 
    } 
    } 
} 

讓我知道它是否適合你。