简体   繁体   中英

servlet not responding back to ajax request

servlet is not sending back response to ajax code. Plaease help!!!

html code, here output should be printed

this is ajax code in javascript

 <script language="javascript">
    reqObj=null;

    function getPrice(){

        if(window.XMLHttpRequest){
              reqObj=new XMLHttpRequest();
           }else {
              reqObj=new ActiveXObject("Microsoft.XMLHTTP");
          }

            reqObj.onreadystatechange=process;


           var area = document.getElementById('product').value;
           var fType= document.getElementById('size').value;




         reqObj.open("POST","./getPricefromSize?pro="+area+"&size="+fType,true);
        reqObj.send(null);


    }
    function process1(){

        if(reqObj.readyState==4){
           var prce=reqObj.responseText;

           document.getElementById("price").innerHTML=prce;

        }

    }


    </script>

this is my servlet code:

  String str=request.getParameter("pro");
    String str1=request.getParameter("size");


     PrintWriter  out1=response.getWriter();


      System.out.println("pro: "+str+"size: "+str1);

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       con=DriverManager.getConnection("jdbc:odbc:linpaws","system","oracle");
       st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        rs=st.executeQuery("select price from labpro where usernm='"+labid+"' and product='"+str+"' and sze='"+str1+"'");
      rs.first();
        price=rs.getString(1);


        System.out.println("price"+price);
       out1.write(price);

       rs.close();
    st.close();

output is printed in console. But not showing in ajax call

You are missing some bits out of your code:

xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
   }
 }
xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");

http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp should put you on the right track.

Another reason its not working is your assigning process to your onreadystatechange eg onreadystatechange=process but process must exactly match the name of the function your assigning which in your case is process1 so the code would become reqObj.onreadystatechange=process1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM