JS Function:
function loadAjax1(y){
var r=document.getElementById(y);
var id=r.children[0].value;
var name=r.children[1].value;
var location=r.children[2].value;
var nbed=r.children[3].value;
var obed=r.children[4].value;
var ibed=r.children[5].value;
var url="Ajax";
var data = `{
"data":[
{
"type":"hospitals",
"id":"12",
"attributes":{
"hid":"113",
"hospital_name":"RKO",
"location":"DEF",
"normal_beds":"9",
"oxygen_beds":"10",
"icu_beds":"12"
}
}
]
}`;
console.log(data);
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}else if(window.ActiveXObject){
request = new ActiveXObject("Microsoft.XMLHTTP");
}
try{
request.onreadystatechange=sendInfo1;
request.open("POST",url,true);
request.setRequestHeader("Content-Type", "application/vnd.api+json");
request.send(JSON.stringify(data));
}catch(e){
alert("Unable to connect server");
}
}
This is my code for AJAX call. Here I am making a POST request with XMLHTTP request object and this request is sent to a servlet. How can I read the request body in the servlet? I have tried with request.getParameter(). But it is not working and it says that the string is null. How can I fetch the data in request body and PARSE it as JSON.
Thanks in Advance