Created
February 15, 2021 16:59
-
-
Save tiwariayush/a078246e368067de9c28edfe35ba4ebd to your computer and use it in GitHub Desktop.
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
| //Send a request | |
| //Must send methid as a string, url as a string, data as an object, headers as an array of objects | |
| window.sendRequest = (method,url,data,headers) =>{ | |
| let xhr = new XMLHttpRequest(); | |
| let json = JSON.stringify(data); | |
| xhr.open(method,url); | |
| if (method == 'POST'){ | |
| xhr.setRequestHeader('Content-type', 'application/json'); | |
| } | |
| for (h in headers){ | |
| header = headers[h]; | |
| xhr.setRequestHeader(header.key, header.value); | |
| } | |
| xhr.send(json); | |
| xhr.onload = () => { | |
| if (xhr.status != 200) { // analyze HTTP status of the response | |
| console.log(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found | |
| } | |
| return; | |
| }; | |
| } | |
| //Base64 Encode data | |
| window.base64Encode = (data) => { | |
| let encodedData = window.btoa(data); | |
| return(encodedData); | |
| } | |
| //JSON Encode data | |
| window.jsonEncode = (data) => { | |
| let encodedData = JSON.stringify(data); | |
| return(encodedData); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment