Rohit khandelwal
Rohit khandelwal

Reputation: 29

success callback function is not called when ajax call to servlet

I am trying to make dependent drop down list. For that I have created a jsp page and in that calling servlet through ajax call .In the servlet I am using Json object for returning values for dropdown . Values are correctly coming in Json object But The request is getting completed with some error (as success method is not getting called instead error method is called ) .

Here is my ajax code :

$.ajax({ type: "GET",   
             url: "MyServlet?index="+listindex, 
             datatype: 'JSON', 
              error : function() {

            alert("Error");
              },
            success : function(data) {
       try{
        var citiArray=JSON.parse(data);

        if(citiArray != null){

        for(var s=0;s<citiArray.length;s++){

        var serial=citiArray[s];
        //populating drop down list
           $("#dname").append($("<option></option>").val(serial.authors1).html(serial.authors1));
        }
        }
        }
             catch(err){
             alert(err);
                 }
            }
     });

My Servlet code :MyServlet.java

 public class MyServlet extends HttpServlet {
   /**
 * @see HttpServlet#HttpServlet()
 */
public MyServlet() 
{
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   try { 

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con =DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/orcl" ,"hr", "password");

     request.setCharacterEncoding("utf8");
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json"); 
    PrintWriter out = response.getWriter(); 

    String listindex = request.getParameter("index");
    out.print(listindex);
  String query2 = "select module from course where cname=?";
 PreparedStatement st2 = con.prepareStatement(query2);
 st2.setString(1,listindex);
 ResultSet rs2=st2.executeQuery();

  JSONArray uniList1 = new JSONArray();
  while (rs2.next()) 
   {
       uniList1.add(rs2.getString("module"));
       //System.out.println(rs2.getString("module"));
    }


    JSONObject obj = new JSONObject();
    obj.put("key", uniList1 );
    System.out.println(obj);
    out.print(obj);
  }
   catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    }
}

web.xml:

  <servlet-name>MyServlet</servlet-name>  
  <servlet-class>com.dac.MyServlet</servlet-class>  
  </servlet>  

  <servlet-mapping>  
  <servlet-name>MyServlet</servlet-name>  
  <url-pattern>/MyServlet</url-pattern>  
  </servlet-mapping>  

Thanks Rohit

Upvotes: 0

Views: 1024

Answers (1)

TheConsultant
TheConsultant

Reputation: 94

Send a response even if an exception is thrown

 catch (Exception e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
    //send the response
    out.print(e.getMessage());
}

You can have a detailed description of the error with:

 error: function (theRequest, theStatus, theError) 
 {
    alert(theRequest.responseText);
}

instead of

  error : function() {

        alert("Error");
          }

Upvotes: 1

Related Questions