JavaScript işlevinin bir isteğine yanıt olarak bazı HTML göndermek için bir Java Servlet almaya çalışıyorum. Ancak, sunucu işlevi çağrılırken ve bir yanıt gönderirken, Javascript işlevleri boş bir String'den başka bir şey elde etmiyor.Java sunucu yanıtı JavaScript'e bildirme
İşteString type = request.getParameter("type");
if(type.equals("locos")) {
response.setContentType("text/html");
//this prints out
System.out.println("Responding with vehicle list");
//deal with response
PrintWriter out = response.getWriter();
out.write("<p>test response</p>"); //finish
}
JavaScript fonksiyonudur:
this.updateVehicleList = function() {
var type = "locos";
var xhr = new XMLHttpRequest();
xhr.open('GET', 'GetList?type=' + encodeURIComponent(type),true);
xhr.send(null);
//deal with response
var res = xhr.responseText;
//for testing
if (res == "") {
window.alert("I'm getting nothing");
}
view.showVehicleList(res);
};
"Hiçbir şey alıyorum" mesajı her zaman verir Burada
Servlet yöntemidir. Servlet'ten yanıt almak için JavaScript’i nasıl alabilirim?
İlgili: [? Servlet ve Ajax nasıl kullanılır] (http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax) çalıştı – BalusC