UNPKG

repetitive.ajax

Version:

Standalone ajax() function as a UMD module from the repetitive.js library. Sends an AJAX request.

2 lines (1 loc) 1.19 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):e.ajax=o()}(this,function(){"use strict";return function(options){if(!(options={type:options.type||"GET",url:options.url||!1,timeout:options.timeout||0,data:options.data||null,onSuccess:options.onSuccess||!1,onTimeout:options.onTimeout||null,onError:options.onError||null}).url)throw new Error("repetitive.ajax error: There's no url property inside the options object!");if(!options.onSuccess)throw new Error("repetitive.ajax error: There's no onSuccess method inside the options object!");var e=new XMLHttpRequest;e.open(options.type,options.url,!0),e.timeout=options.timeout,e.onerror=options.onError,e.ontimeout=options.onTimeout,"POST"===String(options.type).toUpperCase()&&e.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),e.onreadystatechange=function(){if(4===e.readyState&&e.status>=200&&e.status<400){var o={responseText:e.responseText};e.response&&(o.response=e.response),e.responseXML&&(o.responseXML=e.responseXML),e.responseURL&&(o.responseURL=e.responseURL),options.onSuccess(o)}},e.send(options.data)}});