I hav done Ajax code for dynamically select destination city according to user selected source city. My issue is this code work properly on my pc but whenever copied on other pc its not working. I can't understand so please help me.


Ajax code is

<script type="text/javascript">

            //window.onload="hideReturn()"
            function hideReturn()
                {
                    document.getElementById("noPaengersRe").style.display="none"
                    document.getElementById("listNumOfPersonRe").style.display="none"
                }

 
            function ListOfDest(selectedCity)
            {
               // alert("function load")
                xmlHttp = GetXmlHttpObject();
               // alert("xml obj is "+xmlHttp)
                if(xmlHttp == null)
                    {
                        alert("Browser does not support HTTP Request")
                        return
                    }
                var url = "DestinationRe"
                url = url+"?cityFrom="+selectedCity
               // alert("step url is "+url)
                xmlHttp.onreadystatechange=stateChanged
                xmlHttp.open("GET",url,true)
               // alert("step 2")
                xmlHttp.send()
               // alert("step 3")
            }
            function stateChanged()
            {
            //    alert("state is load")
                document.getElementById("listTo").options.length=0;
                AddItemlistTo("----Select City----","----Select City----")
               // if (xmlhttp.readyState==4 && xmlhttp.status==200)
                if(xmlHttp.readyState == 4 && xmlHttp.status==200)
                    {
                        //alert("step 4")
                        var strCity = xmlHttp.responseText;
                        //alert("str city is "+strCity)
                        var listCity = strCity.split(":");
                        //var listCity = strCity.split(":");
                     //   alert("list city is "+listCity)
                        if(listCity.length == 1)
                            {
                                AddItemlistTo(strCity,strCity)
                                document.getElementById("listTo").focus()
                            }else{
                                var len = listCity.length;
                                for(var i = 0;i<len;i++)
                                    {
                                       // alert("Item is "+listCity[i]+" length is "+listCity[i].length+" value of i is "+i)
                                        if(i != len-1)
                                            {
                                                AddItemlistTo(listCity[i],listCity[i])
                                            }else{
                                                var lenCity = listCity[i].length;
                                              //  alert("lenCity is "+lenCity)
                                                var lastCity = listCity[i].substring(0,lenCity-2)
                                            //    alert("lastCity len is "+lastCity.length)
                                                AddItemlistTo(lastCity,lastCity)
                                            }
                                    }
                                    document.getElementById("listTo").focus()
                            }
                    }
            }

            function GetXmlHttpObject()
            {
                var xmlHttp = null;
                try
                {
                    // Firefox, Opera 8.0+, Safari
                    xmlHttp = new XMLHttpRequest();
                }
                catch(e)
                {
                    try
                    {
                        xmlHttp = new ActiveXObject("Msxl2.XMLHTTP");
                    }
                    catch(e)
                    {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
                    }
                }
                return xmlHttp;
            }

            function AddItemlistTo(Text,Value)
            {
                // Create an Option object

                var opt = document.createElement("option");

                // Add an Option object to Drop Down/List Box
                document.getElementById("listTo").options.add(opt);

                // Assign text and value to Option object
                opt.text = Text;
                opt.value = Value;
            }
            function EmptylistTo()
            {
                var opt1 = document.createElement("option");
                document.getElementById("listTo").options.add(opt1);
                opt.text = "----Select City----";
            }
        </script>

Servlet code for processing as followes

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.*;

/**
 *
 * @author node4
 */
public class DestinationRe extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        Connection con = null;
        Statement stmtDestination = null;
        ResultSet rsDestination = null;

        con = (Connection)ConnectionClass.connection();


        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String sourceDis = request.getParameter("cityFrom");
       // String destinationDis = request.getParameter("cityTo");

        String cityData = "";
       ArrayList listDestination = new ArrayList();
            try{
               // out.println("The source is "+sourceDis);
                String sqlDestination = "SELECT DISTINCT(Destination) FROM BusMaster WHERE Source='"+sourceDis+"'";
                stmtDestination = con.createStatement();
                rsDestination = stmtDestination.executeQuery(sqlDestination);
                int count = 0;

                while(rsDestination.next())
                {
                    listDestination.add(rsDestination.getString("Destination"));
                    count++;
                }
                 Iterator itrr = listDestination.iterator();



                 while(itrr.hasNext())
                 {

                   /* if(count == 1)
                    {
                        cityData = itrr.next() + ":";
                    }else{*/
                             if(cityData.equals(""))
                            {
                                cityData =(String) itrr.next();
                            }else{
                                   cityData = cityData + ":" + itrr.next();
                                 }
                  //  }

                 }
                 out.println(cityData);

                /*request.setAttribute("sndlistDestination", listDestination);
                request.setAttribute("sndsource", sourceDis);

                RequestDispatcher dispatcherDist = request.getRequestDispatcher("index.jsp");
                if(dispatcherDist != null)
                {
                    dispatcherDist.forward(request, response);
                }*/

            }catch(Exception e)
            {
                out.println("Excetion regards Destination is "+e);
            }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

My issue is this code work properly on my pc but whenever copied on other pc its not working.

On your working pc, when you load your page, what URL are you entering in the address bar? IF you are entering "http://localhost/...", then the problem is that when you go to another PC, THAT OTHER pc does NOT have a local web server running.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.