首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ajax.Request返回值

使用Ajax.Request返回值
EN

Stack Overflow用户
提问于 2009-06-22 21:27:57
回答 2查看 380关注 0票数 1

当Ajax调用返回值时,为什么这个脚本会导致'undefined‘?

代码语言:javascript
复制
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时遇到了问题。我做错了什么?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2009-06-22 21:47:24

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

这可能更像是你想要的:

代码语言:javascript
复制
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);
        }
      });
    }
  }
}

如果对你有效,请让我知道。

票数 2
EN

Stack Overflow用户

发布于 2009-06-22 21:33:11

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

我猜你可以说值就是对象本身。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1029488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档