[risolto][javascript]Creare uno wrap di XmlHttpRequest

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Avatar utente
disko
Prode Principiante
Messaggi: 217
Iscrizione: sabato 27 ottobre 2007, 16:52

[risolto][javascript]Creare uno wrap di XmlHttpRequest

Messaggio da disko »

ciao vorrei creare uno wrap per sovrascrivere window.XmlHttpRequest aggiungendo del codice a qualche funzione

Codice: Seleziona tutto

<script>
(function() {
PXHR = function() {
    try { this.original = new XMLHttpRequest; } catch (e) {}
    if(!this.original) try { this.original = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) {}
    if(!this.original) try { this.original = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {}
    if(!this.original) try { this.original = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch (e) {}

    var pxhr = this;
    this.original.onreadystatechange = function() {
        pxhr._updateProps();
        return pxhr.onreadystatechange ? pxhr.onreadystatechange() : null;
    };
    this._updateProps();
}

PXHR.prototype = 
{
    abort: function() { return this.original.abort();},
    getAllResponseHeaders: function() {return this.original.getAllResponseHeaders();},
    getResponseHeader: function(header) {return this.original.getResponseHeader(header);},
    overrideMimeType: function(){return this.original.overrideMimeType()},
    send: function(data) {return this.original.send(data);},
    setRequestHeader: function(header, value) {return this.original.setRequestHeader(header, value);},
    open: function(method, url, async, username, password) 
    {
        if(method.toLowerCase() == 'get' && this.sameServer(url) && !url.contains('csrftoken'))
        {
            if(url.contains('?'))
            {
                url += '&csrftokenAjax=1&csrftoken='+this.getToken();        
            }
            else
            {
                url += '?csrftokenAjax=1&csrftoken='+this.getToken();        
            }    
        }
        if (username) 
        {
            return this.original.open(method, url, async, username, password);
        }
        return this.original.open(method, url, async);
    }
}

PXHR.prototype.onreadystatechange = function() {}

PXHR.prototype._updateProps = function() {
    this.readyState = this.original.readyState;
    this.timeout = this.original.timeout;
    this.upload = this.original.upload;
    this.withCredentials = this.original.withCredentials;
    if (this.readyState == 4) {
        this.response = this.original.response;
        this.responseText = this.original.responseText;
        this.responseType = this.original.responseType;
        this.responseXML = this.original.responseXML;
        this.status = this.original.status;
        this.statusText = this.original.statusText;
    }
}

//external vars: server,csrftoken
PXHR.sameServer = function (path)
{
    return path.toLowerCase().contains(server) || !path.toLowerCase().contains('http');
}
PXHR.getToken = function()
{
    return csrftoken;        
}
window.XMLHttpRequest = PXHR;

})();
</script>
tuttavia ho verificato che appena viene eseguito il codice nel costruttore

Codice: Seleziona tutto

this.original.onreadystatechange = function() {}
va in loop anche se la funzione anonima non contiente codice.. perchè? grazie
Ultima modifica di disko il domenica 22 settembre 2013, 18:19, modificato 1 volta in totale.
Avatar utente
Zoff
Moderatore Globale
Moderatore Globale
Messaggi: 33338
Iscrizione: mercoledì 10 ottobre 2007, 22:36

Re: [javascript]Creare uno wrap di XmlHttpRequest

Messaggio da Zoff »

Hai sovrascritto window.XMLHttpRequest poi la usi nel costruttore quindi di fatto richiami il costruttore all'inifinito.
Salvatela in una variabile e usa quella.
Tipo:

Codice: Seleziona tutto

window.oldXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest = PXHR;
poi:

Codice: Seleziona tutto

(function() {
PXHR = function() {
    try { this.original = new window.oldXMLHttpRequest; } catch (e) {}
Prima di aprire una discussione leggi le Guide, poi vedi se c'è un HowTo nel Wiki e fai una ricerca nel Forum!
Applica semplicemente il [Risolto]! Prova: http://forum.ubuntu-it.org/viewtopic.php?f=70&t=548821
Vuoi qualcosa di piu' dal forum? Prova i miei script: http://forum.ubuntu-it.org/viewtopic.php?f=70&t=597066
Avatar utente
disko
Prode Principiante
Messaggi: 217
Iscrizione: sabato 27 ottobre 2007, 16:52

Re: [javascript]Creare uno wrap di XmlHttpRequest

Messaggio da disko »

grazie
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: steff e 5 ospiti