Last active
December 26, 2020 01:32
-
-
Save pedrovasconcellos/8d67b8e970d1ba5671562ecbc27a3fdb to your computer and use it in GitHub Desktop.
Http POST in Ajax using pure Javascript
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
| var url = 'https://udemy.com.br/OpenClass'; | |
| var obj = { schoolID: 77 , classId: 3}; | |
| var callBackParameter = {}; | |
| var callBackFunction = function(ajax, callBackParameter) { | |
| let objResponse = JSON.parse(ajax.responseText); | |
| console.log('Response Object', objResponse); | |
| }; | |
| var callBackErrorParamater = {}; | |
| var callBackErrorFunction = function(ajax, callBackErrorParamater) { | |
| console.log('Fail', ajax.responseText); | |
| }; | |
| function HttpPOST(url, obj, callBackFunction, callBackParameter, callBackErrorFunction, callBackErrorParamater) { | |
| let json = JSON.stringify(obj); | |
| let ajax = new XMLHttpRequest(); | |
| ajax.open('POST', url, true); | |
| ajax.setRequestHeader("Content-type", "application/json"); | |
| ajax.send(json); | |
| ajax.onload = function () { | |
| if (ajax.readyState == 4 && (ajax.status == 200 || ajax.status == 201)) { | |
| if (typeof callBackFunction !== typeof undefined && callBackFunction !== null) | |
| callBackFunction(ajax, callBackParameter); | |
| } | |
| else { | |
| if (typeof callBackErrorFunction !== typeof undefined && callBackErrorFunction !== null) | |
| callBackErrorFunction(ajax, callBackErrorParamater); | |
| } | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment