使用Jquery调用Servlet
我正在尝试使用Jquery从servlet中检索使用Jquery调用Servlet,jquery,servlets,Jquery,Servlets,我正在尝试使用Jquery从servlet中检索 $(document).ready(function() { $("#mybutton").click(function(event) { alert('1'); $.get("http://localhost:8888/ntlm/getuser", function(data) { alert('data ' + data); $(".errorMssg")
$(document).ready(function() {
$("#mybutton").click(function(event) {
alert('1');
$.get("http://localhost:8888/ntlm/getuser", function(data) {
alert('data ' + data);
$(".errorMssg").text("Data Loaded: " + data);
alert('data ' + data);
});
return true;
});
});
在GetUserServlet中,我有
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Authenticating?");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}
如果我直接访问http://localhost:8888/ntlm/getuser
,我可以看到输出,但是当我使用Jquery调用时,无法看到输出
原因可能是什么?执行跨域请求,例如请求
http://localhost:8888/ntlm/authenticate
从http://192.168.1.2:8988
,您需要启用或使用JSONP
.控制台怎么说?@alkis响应为空。空是什么意思?你看到
alert('data')
两次了吗?@PaulGrime我没有看到alert('data')
网页也在http://localhost:8888
?否则浏览器将停止跨域请求(无特殊例外)。请求是否已发送?服务器是否收到请求?感谢Paul提供的解决方案。