发送重复参数时,AJAX调用失败

时间:2013-12-15 12:51:59

标签: java ajax

我面临着非常有趣的问题,问题是 -

在我的应用程序中,有一项功能可以将产品添加到购物车中,我已经使用AJAX实现了这一功能。当我添加不同的项目(例如Item-1,Item-2,...,Item-n)时它可以正常工作,但是当我再次添加相同的项目时(例如Item-1, Item-1 OR Item-1,Item-2,...,Item-n,Item-1)失败

以下是我的AJAX代码 -

   var xmlHttp;
//FUNCTION TO CREATE BROWSER COMPATIBLE OBJECT.
function createBrowserObject()  
{
    if (typeof XMLHttpRequest != "undefined")  //Object for Netscape 5+, Firefox, Opera, Safari,and Internet Explorer 7
    {
            //alert("Obj created");
            xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)  //Version for Internet Explorer 5 and 6.
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
    }
    if (xmlHttp == null)         //Fails on older and nonstandard browsers
    {
        alert("Browser does not support XMLHTTP Request");
    }
}

function addToCartChange() 
{
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") //Check whether Server response came back with no errors.
    {
        document.getElementById("cart_div").innerHTML = xmlHttp.responseText;
    }
}

function addToCart(titleId)
{
    var lastIndex=cartID.substring(parseInt(cartID.lastIndexOf("_"))+1);
    var titleId="product_title_"+lastIndex;
    var priceId="product_price_"+lastIndex;

    var productTitle=document.getElementById(titleId).innerHTML;

    var productPrice=document.getElementById(priceId).innerHTML;
    productPrice=productPrice.substring(parseInt(productPrice.lastIndexOf(";"))+1);
   createBrowserObject();//CREATE BROWSER COMPATIBLE OBJECT.// 

    var url = "../AddToCartServlet";  // URL of server-side resource.//CALL TO THE SERVLET//
    url += "?productTitle=" + productTitle + "&productPrice=" + productPrice;
    xmlHttp.onreadystatechange =addToCartChange;  //ASSIGN RESPONSE HANDLER FUNCTION NAME TO ONREADYSTATECHANGE//.
    xmlHttp.open("GET", url, true);   //INITIATE GET or POST REQUEST. (Here GET)
    xmlHttp.send(null);    // SEND DATA. (Always null in case of GET.)

}

我的AddToCartServlet代码是 -

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    // TODO Auto-generated method stub


    System.out.println("inside AddToCartServlet ");
    String strProductTitle=request.getParameter("productTitle");// TO STORE THE VALUE IN productTitle VARIABLE.//

    String strProductPrice=request.getParameter("productPrice");// TO STORE THE VALUE IN productPrice VARIABLE.//
    AuthenticateServlet.strListItemName.add(strProductTitle);
    AuthenticateServlet.strListItemPrice.add(strProductPrice);

    String strBuffur="<table width='100%' border='0' cellspacing='0' cellpadding='0' class='cart_table'>";
    float floatTotal=0;

    for(int i=0; i < AuthenticateServlet.strListItemName.size(); i++)
    {

        System.out.println("inside loop strListItemName("+i+")= "+AuthenticateServlet.strListItemName.get(i));

        strBuffur=strBuffur + "<tr id='item_"+i+"'>" + "<td width='58%' align='left' valign='middle'><strong><span class='prod_name'>"+AuthenticateServlet.strListItemName.get(i)+"</span></strong></td>"
        + "<td width='19%' align='center' valign='middle'>"
        + "<form id='form1' name='form1' method='post' action=''><label>"
        + "<input type='text' value='1' name='textfield' id='itemQty_"+i+"' class='cart_input' />"
        + "</label></form></td>"
        + "<td width='23%' align='left' valign='middle'><strong>Rs.&nbsp;"+AuthenticateServlet.strListItemPrice.get(i)+"</strong></td></tr>";

        floatTotal=floatTotal+Float.parseFloat(AuthenticateServlet.strListItemPrice.get(i));
    }

    //  System.out.println("Exit for.....");
    strBuffur=strBuffur + "<tr class='total_td'>"
    + "<td colspan='2' align='right' valign='middle'><strong>Total:</strong></td>"
    + "<td align='left' valign='middle'><strong>Rs.&nbsp;&nbsp;"+floatTotal+"</strong></td></tr>"
    + "<tr> <td colspan='3' align='center' valign='middle'> <input name='' type='button' value='Check Out' class='add_cart' onclick='gotoCheckOut()'/></td></tr>"
    + "</table>";
    response.getWriter().println(strBuffur);
}

简而言之,当我再次添加Same项时,对AddToCartServlet的调用失败。请帮忙..谢谢..!

1 个答案:

答案 0 :(得分:0)

如果您用于添加项目名称的CollectionAuthenticateServlet.strListItemNameSet,那么这可能是失败的原因