25531

How to get arraylist data from servlet to jsp using ajax call [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/4112686/how-to-use-servlets-and-ajax" dir="ltr" rel="nofollow">How to use Servlets and Ajax?</a> <span class="question-originals-answer-count"> 7 answers </span> </li> </ul>

I called a servlet through ajax call on widow.load() event ..But when i want to show the value got after success of ajax call in alert box it is showing [object XMLDocument] i dont know why .this is the first time i am using ajax call.

Here is my ajax call code...`

$(window).load(function() { $.ajax({ type: 'GET', url: 'Sites', datatype:'text', success: function(data) { alert(data); debugger; var city=data; for(var i in city) { output ='<input type="checkbox" id="'+city[i]+'" name="'+city[i]+'" value="'+city[i]+'" />'+city[i]+'<br />' } console.log(output) } }); });

And here is my servlet code from where i sending data in arraylist formate.

PrintWriter out = response.getWriter(); ArrayList calltype = new ArrayList(); try { String strQuery = ""; ResultSet rs = null; conexion conexiondb = new conexion(); conexiondb.Conectar(); strQuery = "Select * from sites"; rs = conexiondb.Consulta(strQuery); while (rs.next()) { String toc = rs.getString("sites"); calltype.add(toc); } out.print(calltype); System.out.println(calltype); out.close(); } catch (Exception e) { // display stack trace in the browser System.out.println(e); }

Any help on this will be appreciated .. Thanks in advance..

Answer1:

@Adi what are the values you are receiving in data, like [mumbai,chennai]? Store this values in javascript array variable. Like

var values = []; values = data;

Then you can use jquery .each() <a href="http://api.jquery.com/jQuery.each/" rel="nofollow">jQuery each function</a> to iterate through each cities.

$.each(values, function( index, value ) { alert( index + ": " + value ); });

I haven't checked this code. Please let me know if this helps.

Recommend

  • JPA - getting distinct value from one column
  • php regex remove digits
  • Add delivery info to query in SAP Crystal Reports
  • Update a record where _id = :id with Mongoose
  • CUDA Debugging - VS on windows workstation, GPUs on Linux server?
  • formediting modals shows at wrong position
  • Converting datatype Char to Nvarchar
  • RegistryKey.GetSubKeyNames returns names that are not in that subkey!
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • pyodbc doesn't report sql server error
  • Debugging ASP.NET on a built-in web server suddenly stops
  • MySQL WHERE-condition in procedure ignored
  • Deserializing XML into class C#
  • Web-crawler for facebook in python
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Circular dependency while pushing http interceptor
  • How to delete a row from a dynamic generate table using jquery?
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • using HTMLImports.whenReady not working in chrome
  • How to set the response of a form post action to a iframe source?
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • Change div Background jquery
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • python draw pie shapes with colour filled
  • EntityFramework adding new object to nested object collection
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?