当Ajax调用返回值时,为什么这个脚本会导致'undefined‘?
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;
}
});
}
}}
我正在使用Prototype框架,但在将值返回给object时遇到了问题。我做错了什么?
谢谢!
发布于 2009-06-22 21:47:24
你想要的值在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);
}
});
}
}
}如果对你有效,请让我知道。
发布于 2009-06-22 21:33:11
Ajax.Request不返回任何值,它是一个对象的实例化。
我猜你可以说值就是对象本身。
https://stackoverflow.com/questions/1029488
复制相似问题