Sha256: 2cfd9c95f5a291254a3da38f44f03a77cfb1889315ddd1530e5d2221a650dbfd
Contents?: true
Size: 1.37 KB
Versions: 1
Compression:
Stored size: 1.37 KB
Contents
function buildPromise(xhr) { return new Promise((resolve, reject) => { xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 400) { try { resolve({ data: JSON.parse(xhr.responseText), status: xhr.status }) } catch (error) { reject(error) } } else { try { reject(JSON.parse(xhr.responseText)) } catch (error) { reject(error) } } } xhr.onerror = () => { reject(new Error("An error occurred during the transaction")) } }) } function getToken() { const metaTag = document.querySelector('meta[name="csrf-token"]') return metaTag.attributes.content.textContent } export function get(url, params) { return ajax("GET", url, params) } export function patch(url, data = {}) { return ajax("POST", url, { ...data, _method: "patch" }) } export function post(url, data) { return ajax("POST", url, data) } export default function ajax(method, url, data) { const xhr = new XMLHttpRequest() const promise = buildPromise(xhr) xhr.open(method, url) xhr.setRequestHeader("Content-type", "application/json; charset=utf-8") xhr.setRequestHeader("Accept", "application/json") xhr.setRequestHeader("X-CSRF-Token", getToken()) if (data) { xhr.send(JSON.stringify(data)) } else { xhr.send() } return promise }
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
alchemy_cms-5.3.2 | package/src/utils/ajax.js |