0

I am receiving an Ajax error 404 when calling a java WebServlet. the ajax call is:

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

And the WebServelet is:

@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);
        }
    }
}
Glyn
  • 1,933
  • 5
  • 37
  • 60

2 Answers2

0
  1. replace doPost() to doGet() because your aJax call type: "GET"

  2. replace /CampSiteLocationView.java to /CampSiteLocationView at @WebServlet

Han
  • 263
  • 1
  • 8
0

Your method should call doGet, because you are using GET method in Ajax call.

@Override 
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
Ady Junior
  • 1,040
  • 2
  • 10
  • 18