How should I get the Ajax response from the servlet?
-
I have an Ajax request coming from client side after a key press. The servlet returns a string. How should I grab this string on the client side? It should be split on "," on the client side and display the list. We are using Velocity for rendering the HTML. Servlet code: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String acInfo = request.getQueryString(); SomeDAO dao = new SomeDAO(); ArrayList<String> results = dao.acResults(acInfo); StringBuilder sb = new StringBuilder(); int count = 0; for (String acResult : results) { sb.append(acResult); count++; if (count == results.size()) { break; } sb.append(','); } out.println(sb); out.close(); }
-
Answer:
Dont use "async: false" or it will lose all the AJAX meaning. Do all the stuff you want in the success method. To split by ',', just use split() and to easily iterate arrays use $.each() $.ajax({ type: "GET", url: "/YourServletURL", success : function(text) { var list = text.split(','); $.each(list, function(index, value) { alert(index + ': ' + value); }); // This will show the values. Change "alert" for $('div#mydiv').html(value) or so } });
pushya at Stack Overflow Visit the source
Other answers
If you are not using Jquery then you can use following: <html> <head> <script type="text/javascript"> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; Â } } xmlhttp.open("GET","YOUR_SERVLET_RELATIVE_URL",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">Request data</button> <div id="myDiv"></div> </body> </html>
Sanjay Kumar
Sounds like a simple jQuery ajax response scenario - can't you handle the response with code of following nature ? var responseText = ''; $.ajax({ type: "GET", url: "/YourServletURL", success : function(text) { responseText = text; } }); //alert response or process it or display somewhere alert(responseText);
ring bearer
Related Q & A:
- How can I get an audio alert when I get a new e-mail?Best solution by Yahoo! Answers
- How to I get images to show when I get an email?Best solution by Yahoo! Answers
- How do I get my computer to notify me when I get a email?Best solution by Yahoo! Answers
- How can I get a text alert when I get an email?Best solution by Yahoo! Answers
- How do I post a video response?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.