src/aja.js
function(url, processRes){
                var self = this;

                //iterators
                var key, header;

                var method      = data.method || 'get';
                var async       = data.sync !== true;
                var request     = new XMLHttpRequest();
                var _data       = data.data;
                var body        = data.body;
                var contentType = this.header('Content-Type');
                var timeout     = data.timeout;
                var timeoutId;
                var isUrlEncoded;
                var openParams;

                //guess content type
                if(!contentType && _data && _dataInBody()){
                    this.header('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
                    contentType = this.header('Content-Type');
                }

                //if data is used in body, it needs some modifications regarding the content type
                if(_data && _dataInBody()){
                    if(typeof body !== 'string'){
                        body = '';
                    }

                    if(contentType.indexOf('json') > -1){
                        try {
                            body = JSON.stringify(_data);
                        } catch(e){
                            throw new TypeError('Unable to stringify body\'s content : ' + e.name);
                        }
                    } else {
                        isUrlEncoded = contentType && contentType.indexOf('x-www-form-urlencoded') > 1;
                        for(key in _data){
                            if(isUrlEncoded){
                                body += encodeURIComponent(key) + '=' + encodeURIComponent(_data[key]) + '&';
                            } else {
                                body += key + '=' + _data[key] + '\n\r';
                            }
                        }
                    }
                }

                //open the XHR request
                openParams = [method, url, async];
                if(data.auth){
                    openParams.push(data.auth.user);
                    openParams.push(data.auth.passwd);
                }
                request.open.apply(request, openParams);

                //set the headers
                for(header in data.headers){
                    request.setRequestHeader(header, data.headers[header]);
                }

                //bind events
                request.onprogress = function(e){
                    if (e.lengthComputable) {
                        self.trigger('progress', e.loaded / e.total);
                    }
                };

                request.onload = function onRequestLoad(){
                    var response = request.responseText;

                    if (timeoutId) {
                        clearTimeout(timeoutId);
                    }
                    if(this.status >= 200 && this.status < 300){
                        if(typeof processRes === 'function'){
                            response = processRes(response);
                        }
                        self.trigger('success', response);
                    }

                    self.trigger(this.status, response);

                    self.trigger('end', response);
                };

                request.onerror = function onRequestError (err){
                    if (timeoutId) {
                        clearTimeout(timeoutId);
                    }
                    self.trigger('error', err, arguments);
                };

                //sets the timeout
                if (timeout) {
                    timeoutId = setTimeout(function() {
                        self.trigger('timeout', {
                            type: 'timeout',
                            expiredAfter: timeout
                        }, request, arguments);
                        request.abort();
                    }, timeout);
                }

                //send the request
                request.send(body);
            }
function(url, processRes){
                var self = this;

                //iterators
                var key, header;

                var method      = data.method || 'get';
                var async       = data.sync !== true;
                var request     = new XMLHttpRequest();
                var _data       = data.data;
                var body        = data.body;
                var contentType = this.header('Content-Type');
                var timeout     = data.timeout;
                var timeoutId;
                var isUrlEncoded;
                var openParams;

                //guess content type
                if(!contentType && _data && _dataInBody()){
                    this.header('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
                    contentType = this.header('Content-Type');
                }

                //if data is used in body, it needs some modifications regarding the content type
                if(_data && _dataInBody()){
                    if(typeof body !== 'string'){
                        body = '';
                    }

                    if(contentType.indexOf('json') > -1){
                        try {
                            body = JSON.stringify(_data);
                        } catch(e){
                            throw new TypeError('Unable to stringify body\'s content : ' + e.name);
                        }
                    } else {
                        isUrlEncoded = contentType && contentType.indexOf('x-www-form-urlencoded') > 1;
                        for(key in _data){
                            if(isUrlEncoded){
                                body += encodeURIComponent(key) + '=' + encodeURIComponent(_data[key]) + '&';
                            } else {
                                body += key + '=' + _data[key] + '\n\r';
                            }
                        }
                    }
                }

                //open the XHR request
                openParams = [method, url, async];
                if(data.auth){
                    openParams.push(data.auth.user);
                    openParams.push(data.auth.passwd);
                }
                request.open.apply(request, openParams);

                //set the headers
                for(header in data.headers){
                    request.setRequestHeader(header, data.headers[header]);
                }

                //bind events
                request.onprogress = function(e){
                    if (e.lengthComputable) {
                        self.trigger('progress', e.loaded / e.total);
                    }
                };

                request.onload = function onRequestLoad(){
                    var response = request.responseText;

                    if (timeoutId) {
                        clearTimeout(timeoutId);
                    }
                    if(this.status >= 200 && this.status < 300){
                        if(typeof processRes === 'function'){
                            response = processRes(response);
                        }
                        self.trigger('success', response);
                    }

                    self.trigger(this.status, response);

                    self.trigger('end', response);
                };

                request.onerror = function onRequestError (err){
                    if (timeoutId) {
                        clearTimeout(timeoutId);
                    }
                    self.trigger('error', err, arguments);
                };

                //sets the timeout
                if (timeout) {
                    timeoutId = setTimeout(function() {
                        self.trigger('timeout', {
                            type: 'timeout',
                            expiredAfter: timeout
                        }, request, arguments);
                        request.abort();
                    }, timeout);
                }

                //send the request
                request.send(body);
            }
function ( url , processRes ) { var self = this ; //iterators var key , header ; var method = data . method || 'get' ; var async = data . sync !== true ; var request = new XMLHttpRequest ( ) ; var _data = data . data ; var body = data . body ; var contentType = this . header ( 'Content-Type' ) ; var timeout = data . timeout ; var timeoutId ; var isUrlEncoded ; var openParams ; //guess content type if ( ! contentType && _data && _dataInBody ( ) ) { this . header ( 'Content-Type' , 'application/x-www-form-urlencoded;charset=utf-8' ) ; contentType = this . header ( 'Content-Type' ) ; } //if data is used in body, it needs some modifications regarding the content type if ( _data && _dataInBody ( ) ) { if ( typeof body !== 'string' ) { body = '' ; } if ( contentType . indexOf ( 'json' ) > - 1 ) { try { body = JSON . stringify ( _data ) ; } catch ( e ) { throw new TypeError ( 'Unable to stringify body\'s content : ' + e . name ) ; } } else { isUrlEncoded = contentType && contentType . indexOf ( 'x-www-form-urlencoded' ) > 1 ; for ( key in _data ) { if ( isUrlEncoded ) { body += encodeURIComponent ( key ) + '=' + encodeURIComponent ( _data [ key ] ) + '&' ; } else { body += key + '=' + _data [ key ] + '\n\r' ; } } } } //open the XHR request openParams = [ method , url , async ] ; if ( data . auth ) { openParams . push ( data . auth . user ) ; openParams . push ( data . auth . passwd ) ; } request . open . apply ( request , openParams ) ; //set the headers for ( header in data . headers ) { request . setRequestHeader ( header , data . headers [ header ] ) ; } //bind events request . onprogress = function ( e ) { if ( e . lengthComputable ) { self . trigger ( 'progress' , e . loaded / e . total ) ; } } ; request . onload = function onRequestLoad ( ) { var response = request . responseText ; if ( timeoutId ) { clearTimeout ( timeoutId ) ; } if ( this . status >= 200 && this . status < 300 ) { if ( typeof processRes === 'function' ) { response = processRes ( response ) ; } self . trigger ( 'success' , response ) ; } self . trigger ( this . status , response ) ; self . trigger ( 'end' , response ) ; } ; request . onerror = function onRequestError ( err ) { if ( timeoutId ) { clearTimeout ( timeoutId ) ; } self . trigger ( 'error' , err , arguments ) ; } ; //sets the timeout if ( timeout ) { timeoutId = setTimeout ( function ( ) { self . trigger ( 'timeout' , { type : 'timeout' , expiredAfter : timeout } , request , arguments ) ; request . abort ( ) ; } , timeout ) ; } //send the request request . send ( body ) ; }
Create and send an XHR query.
@param {String} url - the url
@param {Function} processRes - to modify / process the response before sent to events.
Create and send an XHR query .