第一次写ajax,回调函数报404

java(servlet代码页面)

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet")
public class heihie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        resp.getWriter().println("我的名字叫"+name+"我的密码是"+password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

jsp代码页面


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="../js/jquery-3.3.1.js"></script>
    <script type="text/javascript">

        function heihei() {
            $.get(
                "/servlet",
                "name=gege & password=123",
                function (date) {
                    alert(date);
                },
                "callback"
            );
        }
    </script>
</head>
<body>
<input type="button" value="ajax" onclick="heihei()">
  </body>
</html>

点击按钮就会报错

img

404就是请求路径不对,检查下

404就是没有这个接口 看看路径

你的链接不全,加上项目名。

localhost:8080:/servlet/.../*.jsp
404是因为你设置的路径跟访问的路径不一致导致的.

img

这里改成 /servlet/doGet 试试