1

我正在通过ajax从jsp页面向servlet发送一个orderid,如果订单表的orderid不匹配,它将在同一个jsp页面中显示一条错误消息,通过该页面可以正常发出请求。现在我的问题是,当我发送有效的 orderid 时,它应该通过 servlet 将页面转发到另一个 jsp 页面,该页面没有发生并且也没有显示任何错误

jsp页面

  <input type="hidden" name=cmd  value="single"/>
    Enter Order No <input type="text" name="oid" id="oidss" /><input type="button" value="Search" id="finduy" />

jQuery代码

$('#finduy').click(函数(事件){

            $.ajax({
                type:'POST',
                url:"Order",
                style:"full",
                maxRows:12,
                dataType:"json",
                data:{"cmd":"single","oid":encodeURIComponent($('#oidss').val())},

                beforeSend:function(){/*alert("data is sending")*/},
                //problem is here
                success:function(data,textStatus,jqXHR){
                        if(data.success){

                }
                else
                    {
                    alert("invalid Order No");
                    $('#oidss').val("");
                    $('#oidss').focus();


                    }

                },
                error:function(jqXHR, textStatus, errorThrown){
                console.log('textStatus:' + textStatus);
                console.log('errorThrown:' + errorThrown);
                 console.log("Something really bad happened " + textStatus);
                 console.log("jqXHR.responseText " +jqXHR.responseText);

                  $("#ajaxResponse").html(jqXHR.responseText);
            }
            });

小服务程序代码

if(!(cmd==null)&&cmd.equalsIgnoreCase("single")){

        OrderDB odb = new OrderDB();
        Order orders =  new Order() ;
        List<OrderDetail> odetaillist = new ArrayList<OrderDetail>();
        List<Order> ordercustlist = new ArrayList<Order>();


        String orderids = request.getParameter("oid");


        try {
            orders = odb.getOrdersById(orderids);
            odetaillist = odb.getOrdersDetailsByOrderId(orderids);
            ordercustlist = odb.getOrderFromCustomer(orderids);
            Customer customerinfo = odb.getOrderCustomer(orderids);
            request.setAttribute("OrderSingle", orders);
            request.setAttribute("OrderDetail", odetaillist);
            request.setAttribute("OrderCust", ordercustlist);
            request.setAttribute("CustomerInfo", customerinfo);
            redirect=orderinfo;
            RequestDispatcher view = request.getRequestDispatcher(redirect);
            view.forward(request, response);

            if(orders.getOrderid() == null){
                JsonObject myobj =  new JsonObject();
                myobj.addProperty("success", false);
                PrintWriter out = response.getWriter();
                out.print(myobj);
                out.close();
                return;
            }



        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
4

2 回答 2

0

我得到了答案谢谢 user1889970 你的把戏有帮助

我创建了一个新的检查点,其工作只是检查 orderid 是否有效,如果有效则转发到新页面或显示无效

jQuery代码:

 $("#find").click(function(event){
    var cname = $('#oidss').val();
        $.ajax({
            type:'POST',
            url:"Order",
            style:"full",
            maxRows:12,
            dataType:"json",
            data:{"cmd":"find","oid":encodeURIComponent(cname)},

            beforeSend:function(){/*alert("data is sending")*/},
            success:function(data,textStatus,jqXHR){
                    if(data.success){
                        window.location.href='Order?cmd=single&oid='+cname;  
                }
            else
                {
                alert("Invalid Order No.");
                 $('#oidss').val("");
                 $('#oidss').focus();
                }

            },
            error:function(jqXHR, textStatus, errorThrown){
            console.log('textStatus:' + textStatus);
            console.log('errorThrown:' + errorThrown);
             console.log("Something really bad happened " + textStatus);
             console.log("jqXHR.responseText " +jqXHR.responseText);

        }
        });
    });

小服务程序代码:

 else if(!(cmd==null)&&cmd.equalsIgnoreCase("find")){

        OrderDB odb         =   new OrderDB();
        Order orders        =   new Order() ;
        JsonObject myobj    =   new JsonObject();

        PrintWriter out = response.getWriter();
        String orderids = request.getParameter("oid");

        try {orders = odb.getOrdersById(orderids);
        if(orders.getOrderid() != null){
            myobj.addProperty("success", true);
        }
        else{
            myobj.addProperty("success", false);
        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       out.print(myobj);
        out.close();
        return;
    }

cmd=single 的第二部分:

    if(!(cmd==null)&&cmd.equalsIgnoreCase("single")){

        OrderDB odb = new OrderDB();
        Order orders =  new Order() ;
        List<OrderDetail> odetaillist = new ArrayList<OrderDetail>();
        List<Order> ordercustlist = new ArrayList<Order>();
        String orderids = request.getParameter("oid");


        try {
            orders = odb.getOrdersById(orderids);
            odetaillist = odb.getOrdersDetailsByOrderId(orderids);
            ordercustlist = odb.getOrderFromCustomer(orderids);
            Customer customerinfo = odb.getOrderCustomer(orderids);
            request.setAttribute("OrderSingle", orders);
            request.setAttribute("OrderDetail", odetaillist);
            request.setAttribute("OrderCust", ordercustlist);
            request.setAttribute("CustomerInfo", customerinfo);
            redirect=orderinfo;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
于 2013-05-26T03:36:22.703 回答
0

您始终可以从 servlet 发回响应代码,然后执行 forward 。例如,您可以发送成功,在这种情况下,您可以将 document.location 发送到新的 servlet 位置。

现在也可以使用 ajax 在 html5 中上传文件。

于 2013-05-25T21:46:48.403 回答