Last active
August 26, 2020 02:55
-
-
Save Jerrylum/43c5b85612fc9d0c63da26a05a1ffba8 to your computer and use it in GitHub Desktop.
Create a gist so I can download it and use it later :P
This file contains hidden or 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
'use strict'; | |
function getTargetUrl() { | |
return window.location.origin; | |
} | |
function to(url) { | |
window.location.href = url; | |
} | |
async function getData(url = '', data = {}) { | |
var params = new URLSearchParams(); | |
for (var key in data) { | |
params.append(key, data[key]); | |
} | |
const final_url = getTargetUrl() + url + "?" + params.toString(); | |
const response = await fetch(final_url, { | |
method: 'GET', // GET, POST, PUT, DELETE, etc. | |
cache: 'no-cache', // default, no-cache, reload, force-cache, only-if-cached | |
credentials: 'include', // include, same-origin, omit | |
keepalive: true | |
}); | |
return response; | |
} | |
async function sendData(method, url = '', data = {}) { | |
var urlencoded = new URLSearchParams(); | |
for (var key in data) { | |
urlencoded.append(key, data[key]); | |
} | |
const final_url = getTargetUrl() + url; | |
const response = await fetch(final_url, { | |
method, | |
cache: 'no-cache', | |
credentials: 'include', // include cookie | |
headers: { | |
// 'Content-Type': 'application/json' | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
body: urlencoded, | |
keepalive: true | |
}); | |
return response; | |
} | |
async function postData(url = '', data = {}) { | |
return await sendData('POST', url, data); | |
} | |
async function putData(url = '', data = {}) { | |
return await sendData('PUT', url, data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment