diff --git a/src/xr.js b/src/xr.js index 32963a8..a9da9f9 100644 --- a/src/xr.js +++ b/src/xr.js @@ -97,6 +97,16 @@ function xr(args) { true ); + for (const k in opts.headers) { + if (!{}.hasOwnProperty.call(opts.headers, k)) continue; + xhr.setRequestHeader(k, opts.headers[k]); + } + + for (const k in opts.events) { + if (!{}.hasOwnProperty.call(opts.events, k)) continue; + xhr.addEventListener(k, opts.events[k].bind(null, xhr), false); + } + xhr.addEventListener(Events.LOAD, () => { if (xhr.status >= 200 && xhr.status < 300) { let data = null; @@ -115,16 +125,6 @@ function xr(args) { xhr.addEventListener(Events.ERROR, () => reject(res(xhr))); xhr.addEventListener(Events.TIMEOUT, () => reject(res(xhr))); - for (const k in opts.headers) { - if (!{}.hasOwnProperty.call(opts.headers, k)) continue; - xhr.setRequestHeader(k, opts.headers[k]); - } - - for (const k in opts.events) { - if (!{}.hasOwnProperty.call(opts.events, k)) continue; - xhr.addEventListener(k, opts.events[k].bind(null, xhr), false); - } - const data = (typeof opts.data === 'object' && !opts.raw) ? opts.dump(opts.data) : opts.data;