pinafore/routes/_utils/ajax.js
2018-02-08 22:29:29 -08:00

28 lines
629 B
JavaScript

export async function post (url, body) {
return (await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})).json()
}
export function paramsString (paramsObject) {
let params = new URLSearchParams()
Object.keys(paramsObject).forEach(key => {
params.set(key, paramsObject[key])
})
return params.toString()
}
export async function get (url, headers = {}) {
return (await fetch(url, {
method: 'GET',
headers: Object.assign(headers, {
'Accept': 'application/json'
})
})).json()
}