src/ajax.js
function ajax(url, options, method = 'GET') {
    if (typeof url == 'undefined') {
        console.error('No url specified for the ajax.');
        throw new TypeError('A URL is required for making the ajax request.');
    }

    if (typeof options === 'undefined' && typeof url === 'object' && url.url) {
        options = url;
        url = options.url;
    } else if (typeof url !== 'string') {
        console.error('No url/options specified for the ajax.');
        throw new TypeError('Options must be an object for making the ajax request.');
    }

    // default options
    options = Object.assign({}, defaults, options, {method: method});

    console.log(`initiating ajax request... url: ${url}`, ' :: options:', options);

    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();

        // set response type
        if (options.responseType) {
            xhr.responseType = options.responseType;
        }
        // open connection
        xhr.open(
            options.method,
            url,
            typeof options.async === 'undefined' ? true : options.async,
            options.user,
            options.password
        );
        // set headers
        Object.keys(options.headers || {}).forEach(function(name) {
            xhr.setRequestHeader(name, options.headers[name]);
        });
        // listen to the state change
        xhr.onreadystatechange = () => {
            if (xhr.readyState !== 4) {
                return;
            }

            if (xhr.status >= 200 && xhr.status <= 300) {
                resolve(xhr);
            } else {
                reject(xhr);
            }
        };
        // error handling
        xhr.addEventListener('error', () => reject(xhr));
        xhr.addEventListener('abort', () => reject(xhr));
        // send request
        xhr.send(options.data);
    });
}
function ajax(url, options, method = 'GET') {
    if (typeof url == 'undefined') {
        console.error('No url specified for the ajax.');
        throw new TypeError('A URL is required for making the ajax request.');
    }

    if (typeof options === 'undefined' && typeof url === 'object' && url.url) {
        options = url;
        url = options.url;
    } else if (typeof url !== 'string') {
        console.error('No url/options specified for the ajax.');
        throw new TypeError('Options must be an object for making the ajax request.');
    }

    // default options
    options = Object.assign({}, defaults, options, {method: method});

    console.log(`initiating ajax request... url: ${url}`, ' :: options:', options);

    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest();

        // set response type
        if (options.responseType) {
            xhr.responseType = options.responseType;
        }
        // open connection
        xhr.open(
            options.method,
            url,
            typeof options.async === 'undefined' ? true : options.async,
            options.user,
            options.password
        );
        // set headers
        Object.keys(options.headers || {}).forEach(function(name) {
            xhr.setRequestHeader(name, options.headers[name]);
        });
        // listen to the state change
        xhr.onreadystatechange = () => {
            if (xhr.readyState !== 4) {
                return;
            }

            if (xhr.status >= 200 && xhr.status <= 300) {
                resolve(xhr);
            } else {
                reject(xhr);
            }
        };
        // error handling
        xhr.addEventListener('error', () => reject(xhr));
        xhr.addEventListener('abort', () => reject(xhr));
        // send request
        xhr.send(options.data);
    });
}
function ajax ( url , options , method = 'GET' ) { if ( typeof url == 'undefined' ) { console . error ( 'No url specified for the ajax.' ) ; throw new TypeError ( 'A URL is required for making the ajax request.' ) ; } if ( typeof options === 'undefined' && typeof url === 'object' && url . url ) { options = url ; url = options . url ; } else if ( typeof url !== 'string' ) { console . error ( 'No url/options specified for the ajax.' ) ; throw new TypeError ( 'Options must be an object for making the ajax request.' ) ; } // default options options = Object . assign ( { } , defaults , options , { method : method } ) ; console . log ( ` ${ url } ` , ' :: options:' , options ) ; return new Promise ( ( resolve , reject ) => { let xhr = new XMLHttpRequest ( ) ; // set response type if ( options . responseType ) { xhr . responseType = options . responseType ; } // open connection xhr . open ( options . method , url , typeof options . async === 'undefined' ? true : options . async , options . user , options . password ) ; // set headers Object . keys ( options . headers || { } ) . forEach ( function ( name ) { xhr . setRequestHeader ( name , options . headers [ name ] ) ; } ) ; // listen to the state change xhr . onreadystatechange = ( ) => { if ( xhr . readyState !== 4 ) { return ; } if ( xhr . status >= 200 && xhr . status <= 300 ) { resolve ( xhr ) ; } else { reject ( xhr ) ; } } ; // error handling xhr . addEventListener ( 'error' , ( ) => reject ( xhr ) ) ; xhr . addEventListener ( 'abort' , ( ) => reject ( xhr ) ) ; // send request xhr . send ( options . data ) ; } ) ; }
A function to perform ajax requests. It returns promise instead of relying on callbacks.

@example
ATV.Ajax('http://api.mymovieapp.com/movies')
.then((response) => // do something with the response)
.catch((error) => // catch errors )

@memberof module:ajax

@param  {String} url                                 Resource url
@param  {Object} [options={responseType: 'json'}]    Options to apply for the ajax request
@param  {String} [method='GET']                      Type of HTTP request (defaults to GET)
@return {Promise}                                     The Promise that resolves on ajax success
A function to perform ajax requests . It returns promise instead of relying on callbacks .