Using POST method in XMLHTTPRequest – Ajax
In this example, I am taking data from a form and posting on web API.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script>
function savedata()
{
var params = "name="+document.getElementById('name').value+"&fname="+document.getElementById('fname').value; var obj=new XMLHttpRequest();
obj.open('POST', 'http://apitest.cegrajasthan.in/api/infoes/Postinfo');
obj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
obj.onreadystatechange = function()
{
if(this.readyState == "4" && this.status==200){
document.getElementById("p1").innerHTML = this.responseText;
}
}
obj.send(params);
}
</script>
</head>
<body>
<form action="http://apitest.cegrajasthan.in/api/infoes/Postinfo">
name <input type="text" name="name" id="name"><br>
Father Name <input type="text" name="fname" id="fname"><br>
<input type="button" value="Save" onclick="savedata()">
</form>
<p id="p1"></p>
</body>
</html>