dimanche 26 juin 2016

Getting HTML instead of JSON from xmlhttp.responseText

im new to AJAX and i got the following problem. I want to receive a JSON Array from my Servlet and use the JSON Objects in my JavaScript file. I think i didn't really understood the concept behind that and google did not help me very well. Here are the 2 Snippets of the Javascript and the Servlet.

Javascript:

var xmlhttp = new XMLHttpRequest();
var kaljson;
xmlhttp.onreadystatechange = function(){
   if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      console.log(xmlhttp.responseText);
      kaljson = JSON.parse(xmlhttp.responseText);
   }
}
xmlhttp.open("GET","KalenderFaerben",true);
xmlhttp.send();

for(var i = 0; i < kaljson.length; i++){
   alert(kaljson[i].datum);
}

Servlet:

HttpSession session = request.getSession();
ArrayList<NotizBean> kalender = (ArrayList<NotizBean>) session.getAttribute("kalender");
String kalenderArray = "[";
for (int i = 0; i < kalender.size(); i++) {
    kalenderArray += "{\"datum\":\"" + kalender.get(i).getDatum()+"\"}";
    if(i < (kalender.size()-1))
        kalenderArray += ",";
}
kalenderArray += "]";
System.out.println(kalenderArray);
request.setAttribute("kalenderJson", kalenderArray);

RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/kalender.jsp");
dispatcher.forward(request, response);

Where am i wrong? Hope someone can help me.




Aucun commentaire:

Enregistrer un commentaire