J'écris une demande Http sans l'utilisation d'une bibliothèque (un autre script avait des conflicts ...)Utilisation de My Own Callback avec un objet HttpRequest
Mais Im ayant des problèmes avec la portée de mon objet. Voici le script appelant, puis l'objet Ajax_Request suit.
function loadCard(e) {
var element = e.target;
if($('overlay')) {
return false; //something is already over the layout
}
var card = '/card/'+element.id;
var option = {method:'post', parameters:'test', async:true}
loadOverlay();
var ajax = new Ajax_Request(card, option);
}
// Ajax_Request
function Ajax_Request(url, options) {
if(typeof url !== 'undefined') {
this.url = url;
}
if(typeof options.method !== 'undefined') {
this.method = options.method;
} else {
this.method = 'get';
}
if(typeof options.parameters !== 'undefined') {
this.parameters = options.parameters;
}
if(typeof options.async !== 'undefined') {
this.async = true;
} else {
this.async = false;
}
if(window.XMLHttpRequest) {
this.request = new XMLHttpRequest();
} //check for MS browser
this.makeRequest = function() {
try {
this.request.onreadystatechange = this.checkReadyState;
this.request.open(this.method, this.url, this.async);
if(this.method == 'post') {
this.request.send(this.parameters);
} else {
this.request.send(null);
}
} catch(err) {
alert(err);
}
}
this.setResponse = function(r) {
alert(r)
this.response = r;
}
this.getResponse = function() {
return this.responseText;
}
this.checkReadyState = function(r) {
switch(this.readyState) {
case 4:
//Represents a "loaded" state in which the response has been completely received.
if(this.status == 200) {
this.setResponse(this.responseText)
}
...
}
}
}
Je suis en train de régler la réponse à une propriété si mon objet appelant peut travailler avec elle. Mais quand j'essaie d'appeler this.setResponse(), j'obtiens une erreur qui n'est pas définie. Comment puis-je lier correctement le rappel onreadystatechange à mon programme?
Le script renvoie sinon les données correctement, et je pourrais simplement le sortir juste là, mais j'ai besoin d'un peu plus de flexibilité.
Merci Rich
Cela ne fonctionne pas, pour la même raison que je perds la connexion avec mon objet. this.request n'est pas défini dans la fonction de rappel checkReadyState. –