我从Servlet开始,我不明白我的问题。 我称之为Servlet感谢JQuery,我希望Servlet将数据返回给JS。
我的JS代码:
$.ajax({
url:'http://localhost:8080/hello/servlet',
data:'{field:"abc"}',
type:'post',
cache:false,
success:function(data){alert(data);},
error:function(){alert('error');}
}
);
和我的Servlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException
{
//Do nothing import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException
{
//Do nothing
}
protected void doPost(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException
{
String field = req.getParameter("field");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Access-Control-Max-Age", "86400");
out.println("<html>");
out.println("<body>");
out.println("You entered \"" + field + "\" into the text box.");
out.println("</body>");
out.println("</html>");
}
}
servlet什么都不返回...... 问题是什么?
仅供参考,当我在浏览器中访问localhost:8080 / hello / servlet时,它可以工作。