Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>XMLHttpRequest</code> is an <em>asynchronous</em> request by default, meaning that after you call <code>send</code>, <em>the next line of code is executed before the request has finished</em>. The code to be executed when the request finished goes into your "state change" handler. You're already assigning a state change handler here:</p> <pre><code>xmlHttp.onreadystatechange = handleRequestStateChange </code></pre> <p>But you never define what <code>handleRequestStateChange</code> actually is. You should probably <a href="https://developer.mozilla.org/en/AJAX/Getting_Started#section_3" rel="noreferrer">read about ready states and the onreadystatechange event</a>.</p> <p>Try the following code instead:</p> <pre><code>&lt;button type="button" onClick="handleButtonClick();"&gt;Click Me!&lt;/button&gt; &lt;script type="text/javascript"&gt; function handleButtonClick() { // Declare the variables we'll be using var xmlHttp, handleRequestStateChange; // Define the function to be called when our AJAX request's state changes: handleRequestStateChange = function() { // Check to see if this state change was "request complete", and // there was no server error (404 Not Found, 500 Server Error, etc) if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { var substring=xmlHttp.responseText; // Do something with the text here alert(substring); } } xmlhttp = new XMLHttpRequest(); xmlHttp.open("GET", "http://csce:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true); xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null); } &lt;/script&gt; </code></pre> <p>This code assigns to <code>handleRequestStateChange</code> a function which will be called when the ready state of the <code>XMLHttpRequest</code> changes. The function checks to see if the ready state is <code>4</code> (request finished and response is ready) and if so, the you will be able to access the responseText property of the request which will contain the response from the JSP page.</p> <p>As a suggestion for the future, when you've learned how AJAX request work, you may find a helper library like <a href="http://jquery.com/" rel="noreferrer">jQuery</a> makes tasks like this much easier! </p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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