显示警报调用ajax

时间:2018-10-01 11:24:22

标签: javascript jquery servlets

我正在使用Java Servlet进行服务器端编程,我返回的Json对象是通过ajax调用获得的。我只想显示一个警报框,如果在用户输入的日期之间没有找到数据,则警报文本应出现在我的jsp页面上。

这是我的JSP和servlet代码,我正在使用某种方法来做到这一点,但没有成功,请任何人指导我。

jsp(我正在使用JSP作为视图)

<form id="formid" action="BarServlet" method="get">
    <div class="container">
        <h4>Start Date:</h4>
        <input type="text" id="startdate" name="fromdate" width="276"
            placeholder="dd/mm/yyyy" required />
        <h4>End Date:</h4>
        <input type="text" id="enddate" name="todate" width="276"
            placeholder="dd/mm/yyyy" required />


        <h4>Outlets:</h4>
        <select id="all" name="outlet">
            <option>ALL</option>
            <c:forEach var="item" items="${obj.outlet}">
                <option>${item}</option>
            </c:forEach>
        </select>
        <h5 class="NoDataFound"><%=(request.getAttribute("NoDataFound") == null) ? "" : request.getAttribute("NoDataFound")%></h5>
        <div>
            <br>
             <button class="btn btn-default" type="submit">Search</button>
        </div>
    </div>
</form>

这是我的servlet

受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)

        try {
            con = DBConnection.createConnection();
            statement = con.createStatement();

            ResultSet resultSet = statement.executeQuery(sql);
            if (!resultSet.isBeforeFirst() ) {    
                   request.setAttribute("NoDataFound", "NO Data Found"); 
                      RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp.jsp");
                      dispatcher.forward( request, response);


            }
            while (resultSet.next()) {


> if data is found them this will execute

                list.add(map);
                str = gson.toJson(list);

            }
//System.out.println(list);
            response.setContentType("application/json");

            response.getWriter().write(str);



}

这是我的JavaScript文件

$(document).ready(
    function() {
        $("#formid").submit(
                function(event) {
                    event.preventDefault(); // to stop form being submitted because it reloads the page.
                    $.ajax({
                        url : "BarServlet",
                        method : "GET",
                        data : {
                            fromdate : $("#startdate").val(),
                            todate : $("#enddate").val(),
                            outlet : $("#all").val()
                        },
                        success : function(data) {
                            $("#formid").hide(); // this will hide the form.
                            var paymentmode = [];
                            var amount = [];
                             console.log(data)
                            for ( var i in data) {
                                paymentmode.push(data[i].CashamountX);
                                amount.push(data[i].CashamountY);

                                paymentmode.push(data[i].SwiggyAmtX);
                                amount.push(data[i].SwiggyAmtY);

                                paymentmode.push(data[i].KBAmtX);
                                amount.push(data[i].KBAmtY);

                                paymentmode.push(data[i].BigBasketAmtX);
                                amount.push(data[i].BigBasketAmtY);

                                paymentmode.push(data[i].ZomatoAmtX);
                                amount.push(data[i].ZomatoAmtY);

                                paymentmode.push(data[i].CreditCardAmtX);
                                amount.push(data[i].CreditCardAmtY);

                                paymentmode.push(data[i].CouponAmtX);
                                amount.push(data[i].CouponAmtY);

                                paymentmode.push(data[i].PayTMAmtX);
                                amount.push(data[i].PayTMAmtY);

                                paymentmode.push(data[i].CreditAmtX);
                                amount.push(data[i].CreditAmtX);
                            }
                             Chart.defaults.global.legend.display = false;
                            var chartdata = {
                                labels : paymentmode,
                                datasets : [ {

                                    backgroundColor : [ "#87CEFA",
                                                        "#B0C4DE",
                                                        "#AFEEEE",
                                                        "#00BFFF",
                                                        "#87CEFA",
                                                        "#1E90FF",
                                                        "#708090",
                                                        "#778899",
                                                        "#696969",
                                                        "#808080" ],
                                                        borderColor : 'black',
                                    hoverBackgroundColor : '    #9370DB',
                                    hoverBorderColor : 'red',
                                    data : amount
                                } ],


                            };

                            var ctx = $("#mycanvas");

                            var barGraph = new Chart(ctx, {
                                type : 'bar',
                                data : chartdata
                            });
                        }
                    });
                });
    });

0 个答案:

没有答案