最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - The value of 'this' in a callback function - Stack Overflow

programmeradmin2浏览0评论

I have this code for doing an ajax request to a webservice:

var MyCode = {
    req: new XMLHttpRequest(), // firefox only at the moment

    service_url: "http://url/to/Service.asmx",

    sayhello: function() {
        if (this.req.readyState == 4 || this.req.readyState == 0) {
            this.req.open("POST", this.service_url + '/HelloWorld', true);
            this.req.setRequestHeader('Content-Type','application/json; charset=utf-8');
            this.req.onreadystatechange = this.handleReceive; 
            var param = '{}';
            this.req.send(param);
        }
    },

    handleReceive: function() {
        if (this.req.readyState == 4) {
            // todo: using eval for json is dangerous
            var response = eval("(" + this.req.responseText + ")");
            alert(response);
        }
    }
}

It is called with MyCode.sayhello() of course.

The problem with it is that "req is not defined" at the first line in the handleReceive function. It does get called 4 times, so I know the code above sends the request to the server.

How can I solve this?

I have this code for doing an ajax request to a webservice:

var MyCode = {
    req: new XMLHttpRequest(), // firefox only at the moment

    service_url: "http://url/to/Service.asmx",

    sayhello: function() {
        if (this.req.readyState == 4 || this.req.readyState == 0) {
            this.req.open("POST", this.service_url + '/HelloWorld', true);
            this.req.setRequestHeader('Content-Type','application/json; charset=utf-8');
            this.req.onreadystatechange = this.handleReceive; 
            var param = '{}';
            this.req.send(param);
        }
    },

    handleReceive: function() {
        if (this.req.readyState == 4) {
            // todo: using eval for json is dangerous
            var response = eval("(" + this.req.responseText + ")");
            alert(response);
        }
    }
}

It is called with MyCode.sayhello() of course.

The problem with it is that "req is not defined" at the first line in the handleReceive function. It does get called 4 times, so I know the code above sends the request to the server.

How can I solve this?

Share Improve this question asked May 8, 2009 at 11:28 TominatorTominator 1,2243 gold badges20 silver badges37 bronze badges 1
  • Should be this.responseText .. not this.req.responseText (since 'this' refers to the XHR object) – James Commented May 8, 2009 at 11:40
Add a ment  | 

4 Answers 4

Reset to default 5

Classic closure problem. When you get the callback, the closure actually refers already to the HTTP object.

You can do the following as someone suggests:

var that = this;
this.req.onreadystatechange = function() { this.handleReceive.apply(that, []); };

OR just do the following:

var that = this;
this.req.onreadystatechange = function() { that.handleReceive(); };

You can solve that by makeing a variable refering to this in MyCode. Like

var MyCode = {
    req: new XMLHttpRequest(), // firefox only at the moment

    self = this

    ...
}

Then you can refer to self in stead of this.

Change this:

this.req.onreadystatechange = this.handleReceive;

to this:

var self = this;
this.req.onreadystatechange = function() { self.handleReceive(); }

This creates a closure that should fix your problems.

you should be able to make it work by changing

this.req.onreadystatechange = this.handleReceive;

to

var that = this;
this.req.onreadystatechange = function() { this.handleReceive.apply(that, []); };

Function.prototype.apply can be used to call a function while explicitly this and the function's arguments.

发布评论

评论列表(0)

  1. 暂无评论