Fetch API

Error handling

try {
    let r = await fetch("/foo")
    if (!r.ok) { throw r }
    let data = await r.json()
} catch (e) {
    alert(e.statusText || e.message)
}

Post json

    try {
        let r = await fetch("/foo", {
            method: "POST",
            body: JSON.stringify(payload),
            credentials: "same-origin",
            headers: {
                "Content-Type": "application/json"
            }
        })
        if (!r.ok) { throw r }
        let data = await r.json()
    } catch (e) {
        alert(e.statusText || e.message)
    }