我正在尝试让 jQuery 移动应用程序从我在另一台计算机上运行的服务器读取数据。当我加载页面时,我收到“加载数据时出错”。消息而不是我的“Hello World”
元素。在 Chrome 的 JavaScript 控制台中,我被告知在我的服务器的“Hello World”响应中有“Uncaught SyntaxError: Unexpected identifier”。
我被卡住了,有什么建议吗?我的 js 和我的 Jetty 服务器的代码粘贴在下面:
这是我的 JavaScript/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>
这是我的Java代码:
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);
}
}