当调用java WebServlet[重复]时,我收到一个Ajax错误404。

问题描述 投票:0回答:1

当调用java WebServlet时,我收到一个Ajax错误404。

var csID = $(this).find('option:selected').attr("name");
alert("csID: " + csID);
$.ajax({
    type: "GET",
    url: "CampSiteLocationView",
    cache: false,
    data : {
        csId: csID,
    },
}).fail(function() {
    $("#updateLocation").val("");
    alert("Failed");//This alert is shown
})
.done(function(campSiteAddress) {
    dataType: "text",

    alert("Completed");

    $("#updateLocation").val(campSiteAddress);
});

而WebServelet是:

@WebServlet("/CampSiteLocationView.java")
public class CampSiteLocationView extends HttpServlet implements Serializable {

    private static final long serialVersionUID = 1L;

    public String encoded_csId = null;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CampSiteLocationView: ");

        //Get the variables
        encoded_csId = request.getParameter("csId");

        //Decrypt variables
        byte[] valueDecoded9 = Base64.decodeBase64(encoded_csId);//decoding part
        String csId = new String(valueDecoded9);

        //Get the list of Camp Sites
        String campSiteLocation = MySQLConnection.getCampSiteLocation(csId);

        if (campSiteLocation == null || campSiteLocation.isEmpty()) {
            response.getWriter().write("No Camp Sites.");
        }else{
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(campSiteLocation);
        }
    }
}
java ajax servlets
1个回答
0
投票
  1. 替换 doPost()doGet() 因为你的aJax调用 type: "GET"

  2. 替换 /CampSiteLocationView.java/CampSiteLocationView@WebServlet


0
投票

你的方法应该调用doGet,因为你在Ajax调用中使用的是GET方法。

@Override 
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
© www.soinside.com 2019 - 2024. All rights reserved.