Sending a POST request in Simple Javascript
So recently i wanted to send a POST request to one of my backends using only simple plain Javascript. Found this code on Quora which worked nicely.
Code :
Code :
- xhr = new XMLHttpRequest();
- var url = "url";
- xhr.open("POST", url, true);
- xhr.setRequestHeader("Content-type", "application/json");
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && xhr.status == 200) {
- var json = JSON.parse(xhr.responseText);
- console.log(json.email + ", " + json.name)
- }
- }
- var data = JSON.stringify({"email":"tomb@raider.com","name":"LaraCroft"});
- xhr.send(data);
Comments
Post a Comment