Note that there are some explanatory texts on larger screens.

plurals
  1. POservlet + ajax + jsp
    primarykey
    data
    text
    <p>Hei, I'm trying to fill a textfield declared in a jsp file with some text after calling a servlet using Ajax. The xmlhttp.responseText contains the text returned by the servlet, but the input with the message id is not filled with that text and I don't know why. Any suggestions? The code used is below. </p> <pre><code>function ajaxFunction() { if(xmlhttp) { var txtname = document.getElementById("txtname"); xmlhttp.open("POST","getname",true); //getname will be the servlet name xmlhttp.onreadystatechange = handleServerResponse; xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.send("txtname=" + txtname.value); //Posting txtname to Servlet } } function handleServerResponse() { //alert(xmlhttp.readyState);//ok if (xmlhttp.readyState == 4) { alert(xmlhttp.status); if(xmlhttp.status == 200) { alert(xmlhttp.responseText); // this is ok document.getElementById("message").innerHTML=xmlhttp.responseText; // doesn't work //document.myForm.message.innerHTML=xmlhttp.responseText; //neither } else { alert("Error during AJAX call. Please try again"); } } } &lt;/script&gt; &lt;body&gt; &lt;form name="myForm" method="POST" action="/ajjax/getname"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Enter Name&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="txtname" id="txtname" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;input type="button" value="Submit" onclick="ajaxFunction();" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="text" name="message" id="message" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/head&gt; &lt;/html&gt; </code></pre>
    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