Created
July 16, 2023 20:27
-
-
Save movahhedi/958147be23884f9a6b82e23404ce042a to your computer and use it in GitHub Desktop.
AdmoPro's never-used SimpleFetch function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function SimpleFetch(url = "", Data = {}) { | |
const fd = new FormData(); | |
fd.append("JsonEncodedAjaxData", JSON.stringify(Data)); | |
for (const [key, value] of Object.entries(Data)) fd.append(key, value as string | Blob); | |
// Default options are marked with * | |
const response = await fetch(url, { | |
method: "POST", // *GET, POST, PUT, DELETE, etc. | |
mode: "cors", // no-cors, *cors, same-origin | |
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached | |
credentials: "same-origin", // include, *same-origin, omit | |
redirect: "follow", // manual, *follow, error | |
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url | |
body: fd, // body data type must match "Content-Type" header | |
headers: { | |
Accept: "application/json", | |
// "Content-Type": "application/json" | |
// "Content-Type": "multipart/form-data", | |
// "Content-Type": "application/x-www-form-urlencoded", | |
}, | |
}); | |
return await response.json(); // parses JSON response into native JavaScript objects | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment