| 123456789101112131415161718192021222324252627282930313233 |
- 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 {Promise<Response | never>}
- */
- export function AjaxReq(url, options) {
- let defaults = {headers: fetchHeaders};
- // Extend default options
- Object.assign(defaults, options);
- return fetch(url, defaults)
- .then(resp => {
- if (resp.status === 200)
- return resp.json();
- else
- return Promise.reject(resp);
- });
- }
|