Я создаю объект json для сохранения данных и затем отправляю их сервлету. Но когда я пытаюсь получить объект и отобразить его содержимое в сервлете Java, я не получаю никакого ответа от сервера. Даже если простое System.out.println()
сообщение в браузере.
angular.module('TestApp').controller('PostController',
function($scope, $rootScope, $http) {
$scope.retriveDataAndConvertToJson = function() {
var collectionJsonData = new Array();
for (var i = 0; i < rowID.length; i++) {
collectionJsonData.push({
"payment_term": document.getElementById("textfield1" + rowID[i])
.getAttribute("value"),
"payment": document.getElementById("textfield2" + rowID[i])
.getAttribute("value"),
"party": document.getElementById("textfield3" + rowID[i])
.getAttribute("value"),
"amount": document.getElementById("textfield4" + rowID[i])
.getAttribute("value")
});
console.log(collectionJsonData[i].payment_term);
console.log(collectionJsonData[i].payment);
console.log(collectionJsonData[i].party);
console.log(collectionJsonData[i].amount);
console.log("==========================");
} //End of For loop
var xhr = new XMLHttpRequest();
var url = "http://" + document.location.host + "/SivaTask/processInfo";
console.log("The url is " + url);
xhr.open("POST", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
var data = JSON.stringify(collectionJsonData);
xhr.send(data);
}
});
Servlet
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String contextName = req.getRequestURI();
String url = contextName.substring(contextName.indexOf('/', 2) + 1);
System.out.println(url);
String jsonData = "";
resp.setContentType("text/plain");
System.out.println("hello world");
PrintWriter out = resp.getWriter();
out.write("The information is : ");
System.out.println("hello world2");
}