Note that there are some explanatory texts on larger screens.

plurals
  1. POServlet --x--> Ajax: Ajax code not receiving servlet response
    primarykey
    data
    text
    <p>I'm unable to figure out what is going on here in this ultra simple example.</p> <p><strong>Problem Summary:</strong> I have a simple servlet that appears to run just fine if I drive it manually... by issuing its URL from the browser. By 'just fine' I mean: I can see in the browser HTML page whatever I write in the servlet response.</p> <p><em>However</em>, if I issue the very same URL via Ajax code, the servlet processes the request fine and even 'appears' to be writing out the response fine ... but, just that I do not see any response on the Ajax client code side and thus neither in my browser HTML page.</p> <p><em>Further</em>, if I make my XHR request syncrhonous, the browser error console shows the following exception:</p> <p><em>Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///home/sd/Desktop/test.html :: callServlet :: line 35" data: no]</em></p> <p><strong>Environment:</strong></p> <p>Browser: Firefox 3.5.3</p> <p>Servlet container: Tomcat 6.0.20</p> <p>OS: Linux / Fedora 11</p> <p><strong>Ajax code:</strong></p> <pre><code>&lt;!-- test.html --&gt; &lt;html&gt; &lt;head&gt; &lt;script&gt; var req; function $(id) { return document.getElementById(id); } function servletCallback() { var field = $("debugHtmlId"); field.innerHTML += "readyState='" + req.readyState + "'&lt;br&gt; "; field.innerHTML += "status='" + req.status + "'&lt;br&gt; "; field.innerHTML += "responseText='" + req.responseText + "' | &lt;br&gt; "; } req = new XMLHttpRequest(); req.onreadystatechange = servletCallback; function callServlet() { // With async mode off, I get the // Exception listed above. // req.open("GET", "http://localhost:8080/aaa/bbb?f=test", false); req.open("GET", "http://localhost:8080/aaa/bbb?f=test", true); req.send(null); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input id="callserv" type="submit" value="Call Servlet" onclick="callServlet();" /&gt; &lt;span id="debugHtmlId"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Servlet code:</strong></p> <pre><code>// servlet code public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { rsp.setContentType("text/html"); String val = req.getParameter("f"); if(val.equals("test")) { // Increment value. ++_count; // Return value. PrintWriter out = rsp.getWriter(); out.printf("%d\n", _count); out.close(); // This shows up fine in servlet log. System.out.printf("%d\n", _count); } } // This variable is incremented and returned on each call to doGet(). private int _count = 0; } </code></pre> <p><strong>EDIT:</strong></p> <ol> <li><p>Including the result: Here's what I see for example as the value of my innerHTML of my debugHtmlId element.</p> <p>readyState='1' readyState='1' readyState='2' status='0' responseText='' | readyState='4' status='0' responseText='' | </p></li> <li><p>Strange behavior: Notice also that my readystatechange handler is getting <strong>re-entered</strong>! I mean, I was expecting to see readyState='...' status='...' responseText='...' triads for every state change... </p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload