Javascript 从Jquery对servlet的Ajax调用
我尝试使用Jquery.ajax方法访问servlet,如下所示Javascript 从Jquery对servlet的Ajax调用,javascript,jquery,servlets,Javascript,Jquery,Servlets,我尝试使用Jquery.ajax方法访问servlet,如下所示 function ajaxCall() { $.ajax({ type: 'GET', dataType:"html", url: "http://localhost:7001/Macaw/MacawServlet", success:function(data){ alert(data); }, error
function ajaxCall()
{
$.ajax({
type: 'GET',
dataType:"html",
url: "http://localhost:7001/Macaw/MacawServlet",
success:function(data){
alert(data);
},
error:function(){
alert("failure");
}
});
}
Servlet内容:
package servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class MacawServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MacawServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
File input = new File("C:/Users/540893/workspace/Macaw/WebContent/prpsl_txtsrch.html");
Document doc = Jsoup.parse(input,"UTF-8");
Element content = doc.getElementById("text_search");
Element content1 = doc.getElementById("chr_val_stats");
Element content2 = doc.getElementById("chr_val_delta");
out.println(content);
out.println(content1);
out.println(content2);
System.out.println("success");
}
}
每次我调用这个.ajax
函数时,它都会命中servlet并调用.ajax错误:function(){}
为什么不调用
.ajax success:function(){}
?您的js和java文件在同一上下文中吗
如果是,则不需要在url中使用服务器地址和端口。您可以从/Macaw/MacawServlet开始
如果没有,请选中以启用跨浏览器ajax调用
请参阅-您的
MacawServlet
映射到哪个url?