gradefast/gradebook/static/js/utils.js
/**
 * Send a POST request to the server.
 * @param {string} path - The path to send the request to.
 * @param {Object} data - An object representing the POST data. If any value is not a string, it is
 *      run through JSON.stringify.
 */
Send a POST request to the server.
@param {string} path - The path to send the request to.
@param {Object} data - An object representing the POST data. If any value is not a string, it is
run through JSON.stringify.
Send a POST request to the server . @param { string } path - The path to send the request to . @param { Object } data - An object representing the POST data . If any value is not a string it is run through JSON . stringify .
function post(path, data) {
    const fd = new FormData();
    Object.keys(data).forEach((key) => {
        try {
            fd.append(key, typeof data[key] === "string" ? data[key] : JSON.stringify(data[key]));
        } catch (err) {
            console.error("Error stringifying data for " + key, data[key]);
            throw err;
        }
    });
    const xhr = new XMLHttpRequest();
    const getStatus = () => {
        if (xhr.statusText) {
            return `${xhr.status} ${xhr.statusText}`;
        } else {
            return `${xhr.status}`;
        }
    };
    xhr.addEventListener("load", (event) => {
        let jsonData = parseJson(xhr.responseText, path, getStatus(), event);
        if (jsonData && jsonData.status === "Aight") {
        } else {
            reportResponseError(path, getStatus(), JSON.stringify(jsonData, null, 2), event, jsonData);
        }
    }, false);
    xhr.addEventListener("error", (event) => {
        reportRequestError(path, getStatus(), xhr.responseText, event);
    }, false);
    xhr.open("POST", path, true);
    xhr.send(fd);
}
function post ( path , data ) { const fd = new FormData ( ) ; Object . keys ( data ) . forEach ( ( key ) => { try { fd . append ( key , typeof data [ key ] === "string" ? data [ key ] : JSON . stringify ( data [ key ] ) ) ; } catch ( err ) { console . error ( "Error stringifying data for " + key , data [ key ] ) ; throw err ; } } ) ; const xhr = new XMLHttpRequest ( ) ; const getStatus = ( ) => { if ( xhr . statusText ) { return ` ${ xhr . status } ${ xhr . statusText } ` ; } else { return ` ${ xhr . status } ` ; } } ; xhr . addEventListener ( "load" , ( event ) => { let jsonData = parseJson ( xhr . responseText , path , getStatus ( ) , event ) ; if ( jsonData && jsonData . status === "Aight" ) { } else { reportResponseError ( path , getStatus ( ) , JSON . stringify ( jsonData , null , 2 ) , event , jsonData ) ; } } , false ) ; xhr . addEventListener ( "error" , ( event ) => { reportRequestError ( path , getStatus ( ) , xhr . responseText , event ) ; } , false ) ; xhr . open ( "POST" , path , true ) ; xhr . send ( fd ) ; }
Send a POST request to the server.
Send a POST request to the server .
// All good!