pureAJAX

/** * Makes ajax call * @param options object * @param callback function * * Example: d.ajax({ method: 'POST', url: 'http://api.randomuser.me', async: true, data: { user: 'George', password: '1231231' }, success: function (data) { console.log(data); }, error: function (data) { console.log(data); }, done: function (data) { console.log(data); } }); */ function ajax(options) { var xmlhttp = new XMLHttpRequest(), dataSent = ''; if (!options || typeof options !== 'object') { return; } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState === 4 ) { if (xmlhttp.status === 200 && typeof options.success === 'function') { options.success(xmlhttp); } else if (xmlhttp.status == 400 && typeof options.error === 'function') { options.error(xmlhttp); } else if (typeof options.done === 'function') { options.done(xmlhttp); } } }; xmlhttp.open(options.method || 'GET', options.url, options.async || true); xmlhttp.setRequestHeader('Content-type', options.contentType || 'text/plain;charset=UTF-8'); if (options.data) { loop(options.data, function (value, key) { dataSent += dataSent ? '&' : ''; dataSent += bind('{key}={value}', { key: key, value: value }); }); xmlhttp.send(dataSent); } else { xmlhttp.send(); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.