John-sCC / jcc_frontend

Apache License 2.0
0 stars 0 forks source link

Read & Delete with Table Generator - 4/18 #78

Closed aidenhuynh closed 2 months ago

aidenhuynh commented 2 months ago

Functionality

Here is an example of how the code works: just a basic fetch with the delete method.

fetch(`${url}/api/class_period/delete/${id.slice(6)}`, {
        method: 'DELETE',
        mode: 'cors', // no-cors, *cors, same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'include', // include, *same-origin, omit
        headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${localStorage.getItem("jwtToken")}`
        },
    })
    .then(response => {
        if (!response.ok) {
            throw new Error(`HTTP error: ${response.status} ${response.statusText}`);
        }
        if (response.status === 204) return 'No content'; // Common for DELETE
        return response.json();
    })
    .catch(error => {
        console.error('There was a problem with the fetch operation:', error);
    });

Plans