I’m trying to make a jQuery mobile app read data from a server I’m running on another computer. When I load the page, I get my ‘There was an error loading the data.’ message rather than ‘Hello World’ in my
element. And in the JavaScript console in Chrome, I’m being told that there’s ‘Uncaught SyntaxError: Unexpected identifier’ in my server’s ‘Hello World’ response.
I’m stuck, any suggestions? Code for my js and for my Jetty server pasted below:
Here’s my Javscript/jQuery:
<script>
$.ajax({
url: 'redacted-my computer's ip',
dataType: 'jsonp',
timeout: 5000,
success: function(data, status){
$("p").html(data);
}
,
error: function(){
$("p").html('There was an error loading the data.');
}
});
</script>
And here’s my Java code:
public class HelloServlet extends HttpServlet{
private String greeting="Hello World";
public HelloServlet(){}
public HelloServlet(String greeting)
{
this.greeting=greeting;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(greeting);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Server server = new Server(8082);
HelloServlet helloServ = new HelloServlet();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(helloServ), "/*");
try {
server.start();
} catch (Exception ex) {
Logger.getLogger(HelloServlet.class.getName()).log(Level.SEVERE, null, ex);
}
try {
server.join();
} catch (InterruptedException ex) {
Logger.getLogger(HelloServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
“Hello World” is not in jsonp format, which may be what is causing the error.