I got tomcat running on port 8080 and simple servlet:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
public class MyHelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String data = "Hello World from servlet!";
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(data);
}
}
wget on URL to this servelet, retrives a string:
"Hello World from servlet!"
also web browser prints it, so it works, and tomcat's access log shows response '200'
But when im trying to get this string via my javascript:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.getJSON({
type: "GET", url: "http://localhost:8080/examples/MyHelloWorld",
contentType: "text/plain",
error: function(xhr, ajaxOptions, thrownError){
alert(ajaxOptions);
alert(xhr.status);
alert(thrownError);
},
processData: true,
success: function(data, textStatus, jqXHR){ alert(data); }
});
</script>
</script>
</head>
In tomcat's log i can see response 200, but browser does not show anything - just blank page with no content. If I change getJSON
into get
or ajax
i get alerts with:
xhr.status = 0
ajaxOptions = "error"
thrownError = empty
web server is apache and it runs on port 80
Thx for all help