This question already has answers here:
Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”

(11个答案)



How to use Servlets and Ajax?

(7个答案)


上个月关闭。





调用Java WebServlet时收到Ajax错误404。 ajax调用是:

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);
        }
    }
}

最佳答案

doPost()替换为doGet(),因为您的aJax调用type: "GET"
/CampSiteLocationView.java替换为/CampSiteLocationView

07-28 02:12
查看更多