Hi All
I've got a working implementation using an XMLHttpRequest to grab some information from an RDF triplestore through a webservice and display it on the page. However, I'm trying to move to a more object-orientated design for the requests as we need to store certain information alongside the requests. As a result I've built a wrapper object 'XmlReq' which is supposed to build and run everything.
The callback function is within this class, however when I try to call any function withint the wrapper class the readyState of the XMLHttpRequest is always 0. This makes me think it is only making the callback once for some reason. Bizarrely if I put exactly the same callback function outside my class then everything runs perfectly fine again.
It seems to be necessary to wrap the external call inside an annonymous function, whereas trying to make a function call within a class using an annonymous function won't work.
Any ideas much appreciated! Thanks
SenorCai
I've got a working implementation using an XMLHttpRequest to grab some information from an RDF triplestore through a webservice and display it on the page. However, I'm trying to move to a more object-orientated design for the requests as we need to store certain information alongside the requests. As a result I've built a wrapper object 'XmlReq' which is supposed to build and run everything.
The callback function is within this class, however when I try to call any function withint the wrapper class the readyState of the XMLHttpRequest is always 0. This makes me think it is only making the callback once for some reason. Bizarrely if I put exactly the same callback function outside my class then everything runs perfectly fine again.
Code:
function XmlReq(database, query, requestDescription){
this.database = database;
this.requestDescription = requestDescription;
this.url = "";
if(this.database != null)
this.url = "../sparql/?database="+this.database+"result-format=sparql&stylesheet=&query-lang=sparql&query=";
else
this.url = "../sparql/?result-format=sparql&stylesheet=&query-lang=sparql&query=";
this.url += encodeForSparQL(query);
this.xmlrequest = null;
this.executeRequest = executeRequest;
function executeRequest(){
this.xmlrequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari etc
this.xmlrequest = new XMLHttpRequest();
if (this.xmlrequest.overrideMimeType) {
this.xmlrequest.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE
try {
this.xmlrequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
this.xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!this.xmlrequest) {
alert('Unable to create XMLHTTP instance');
return false;
}
//this only gives readyStates of zero:
[COLOR=red]this.xmlrequest.onreadystatechange = this.handleResponse();[/color]
//this works perfectly but the function is outside the class:
//[COLOR=red]this.xmlrequest.onreadystatechange = function() { processXMLReqChange_new(1, "bla bla"); };[/color]
this.xmlrequest.open('GET', this.url, true);
this.xmlrequest.send(null);
}
this.handleResponse = handleResponse;
function handleResponse(){
//this is correct:
alert("Database: "+ this.database + ". Description: "+ this.requestDescription + ". URL: "+this.url);
//this always returns zero:
alert("Ready State: "+this.xmlrequest.readyState);
}
}
It seems to be necessary to wrap the external call inside an annonymous function, whereas trying to make a function call within a class using an annonymous function won't work.
Any ideas much appreciated! Thanks
SenorCai