AJAX与数据库

时间:2018-11-15 09:20:34

标签: ajax jsp

有一个用于记录新航班的jsp表格。我需要使用数据库中的ajax检查航班号的可用性。我正在尝试编写ajax请求,但我是JS的初学者。

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Flight flight = new Flight();        flight.setFlightNumber(Integer.valueOf(req.getParameter("flightNumber")));
    flight.setFromCity(req.getParameter("fromCity"));
    flight.setToCity(req.getParameter("toCity"));

    if (!validation.flightNumberUnique(flightNumber)){
        req.setAttribute(DUPLICATE_FLIGHT_NUMBER, true);
        forward(Constants.Pages.Admin.ADD_FLIGHT_JSP, req, resp);
        return;
    }

    getFlightService().add(flight);

    LOGGER.trace("New flight added");
    redirectToAction(Constants.ServletPaths.Admin.ADMIN_ALL_FLIGHT_PATH, req, resp);
}

不使用ajax进行这种形式的Servlet:

XML

如何用ajax代替此操作

1 个答案:

答案 0 :(得分:0)

$("#flightNumber").change(function(){
    var flightnumber = $("#flightNumber").val();
    //get to and from values
    $.post("servletname",{"flightNumber":flightnumber,"fromCity":fromCity,"toCity":toCity}, function(data, status){
    //data contains elements that you print on your servlet
    $("#loginerror").text("your message to be displayed in error field");

    });
});

$(selector).post(URL,data,function(data,status,xhr),dataType)用于ajax发布请求(Jquery)