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?