function addUser(username, email, callback) {
var xhr = new XMLHttpRequest();
var response;
var success = !!Math.round(Math.random());
console.log(success);
if (!success) {
response = JSON.stringify({
success: success,
error: "Oups, something went wrong!",
});
} else {
response = JSON.stringify({
success: success,
user: {
username: username,
email: email,
},
});
}
console.log(response);
xhr.open("POST", "/echo/json/", true);
xhr.onload = function () {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
}
};
xhr.setRequestHeader(
"Content-type",
"application/x-www-form-urlencoded"
);
xhr.send("json=" + response);
}
My function to call post method..I am trying to call this function with username and email as inputs which I will be getting from HTML form inputs.