From 5b5fdfb94395d72516d913d2f815ac31e3cf236e Mon Sep 17 00:00:00 2001 From: nugroho Date: Wed, 21 May 2025 10:48:25 +0700 Subject: [PATCH] base app.js --- assets/js/app.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 assets/js/app.js diff --git a/assets/js/app.js b/assets/js/app.js new file mode 100644 index 0000000..e654ec4 --- /dev/null +++ b/assets/js/app.js @@ -0,0 +1,52 @@ +async function getJson(url,headers={}) { + try { + const response = await fetch(url); + return await response.json(); + } catch { + return ({ + status: 0, + body: {} + }); + } +} +async function getText(url, headers={}) { + try { + const response = await fetch(url); + return ({ + status: response.status, + body: await response.text() + }); + } catch { + return ({ + status: 0, + body: "" + }); + } +} +async function postJson(url,data = {},headers={}) { + try { + headers["content-type"] = "application/json"; + const response = await fetch(url,{method: "post", headers: headers,body: JSON.stringify(data)}); + return await response.json(); + } catch { + return ({ + status: 0, + body: {} + }); + } +} +async function postText(url,data = {},headers={}) { + try { + headers["content-type"] = "application/json"; + const response = await fetch(url,{method: "post", headers: headers,body: JSON.stringify(data)}); + return ({ + status: response.status, + body: await response.text() + }); + } catch { + return ({ + status: 0, + body: "" + }); + } +} \ No newline at end of file