I am working on a j2ee application. In my application, I have a drop-down list (or a Select element). I want to populate this dropdown with JSON data as an Ajax response.
Below is the code I have:
Server-side code (json_source.java) that generates a JSON response .:
package demo.model; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.*; public class json_source extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JsonArray data_json=new JsonArray(); Statement st_loginId=null; ResultSet rs_loginId=null; try { Connection con=null; Class.forName("oracle.jdbc.OracleDriver"); con=DriverManager.getConnection("jdbc:oracle:thin:*************","*****","*****"); con.setAutoCommit(true); st_loginId=con.createStatement(); rs_loginId=st_loginId.executeQuery("select login_id \"LOGIN ID\" from user_access");
and JSON data that was successfully generated using server-side code:
{ "aaData": [{ "value": "login1", "text": "kapils" }, { "value": "login2", "text": "davidn" }, { "value": "login3", "text": "alanp" }] }
and below is the code of my client side (source1.jsp) that generate the ajax request:
(using $ .ajax ()):
<script type="text/javascript"> $(document).ready(function() { $('#id_trial').click(function() { alert("entered in trial button code"); $.ajax({ type: "GET", url:"/demo_trial_application/json_source", dataType: "json", success: function (data) { $.each(data.aaData,function(i,data) { alert(data.value+":"+data.text); var div_data="<option value="+data.value+">"+data.text+"</option>"; alert(div_data); $(div_data).appendTo('#ch_user1'); }); } }); }); }); </script> <body> <div id="div_source1"> <select id="ch_user1" > <option value="select"></option> </select> </div> <input type="button" id="id_trial" name="btn_trial" value="Trial Button.."> </body>
OR Usage ($ .getJSON ()):
$.getJSON("/demo_trial_application/json_source", function (data) { $.each(data.aaData, function (i, data) { var div_data = "<option value=" + data.value + ">" + data.text + "</option>"; alert(div_data); $(div_data).appendTo('#ch_user1'); }); });
Now, when I clicked the button (#id_trial), the server-side code runs successfully and as a result, a JSON object is created. but I don’t get this “JSON response” in the success parameter callback function using jQuery ajax call.
, and besides the jQuery ajax call, I also tried using the $.getJSON function to get the JSON response .. but I did not get the JSON data.
So please tell me if there is any error in my code and how to get JSON data using the code above and fill in the drop down list.
I want to populate my dropdown with JSON data using ajax response. help me sort out this problem ... its very urgent for my application.