| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const fetchHeaders = {
- 'Accept': 'text/html',
- 'Content-Type': 'text/html',
- 'X-Requested-With': 'XMLHttpRequest',
- 'Cache-Control': 'no-cache',
- 'Pragma': 'no-cache',
- 'Expires': '0'
- };
- /**
- * Make an AJAX request.
- *
- * If error, catch return a promise error object:
- *
- * .catch(err => alert(`${err.url}\n\n${err.status} (${err.statusText})`));
- *
- * @param url URL of RESTful api to call
- * @param options the fetch() options
- * @returns Object {Promise, signal: AbortController}
- */
- export function AjaxReq(url, options) {
- let fetchSignal = new AbortController();
- let defaults = {
- headers: fetchHeaders,
- signal: fetchSignal.signal
- };
- // Extend default options
- Object.assign(defaults, options);
- let fetchPromise = fetch(url, defaults)
- .then(resp => {
- if (resp.status === 200)
- return resp.json();
- else
- return Promise.reject(resp);
- });
- // Add some properties to return value
- let ret = {};
- Object.assign(ret, {
- fetchPromise: () => fetchPromise,
- abort: function () {
- fetchSignal.abort()
- }
- });
- return ret;
- }
|