2

in my code responseText is not working. It is supposed to display, text entered in the text box +" :Your request has been seen by syam"

<html> <head id="Head1" runat="server"> <title></title> <script type="text/javascript"> var xmlHttpRequest; function sSignature(str) { xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.onreadystatechange = function() { if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { document.getElementById("target").innerHTML = xmlHttpRequest.responseText; } } xmlHttpRequest.open("GET", "AjaxResponse.aspx?q=" + str, true); xmlHttpRequest.send(); } </script> </head> <body> <form id="form1" runat="server"> <div> enter a string :<input type="text" id="textbox" onkeyup="sSignature(this.value)"/> <span id="target">text should change here</span> </div> </form> </body> </html> 

In the code-behind page, in page_load()

string sRequest = Request.QueryString["q"]; var sResponse = sRequest+ " :Your request has been seen by syam"; Response.Write(sResponse); 
5
  • 1
    Can you do better than "Not working"? What does the Net traffic inspector say? Are there errors in the console? Did you try to see how far in the code it goes before it bugs out? Commented Jan 3, 2012 at 8:50
  • it is giving an error in the line when readystate is 4 document.getElementById("target").innerHTML = xmlHttpRequest.responseText; Commented Jan 3, 2012 at 9:03
  • check your code-behind page, is there you getting query string successfully Commented Jan 3, 2012 at 9:05
  • You should get the responsetText from a callback only..I think so Commented Jan 3, 2012 at 9:23
  • This worked for me with PHP back-end. $sRequest = $_GET["q"]; $sResponse = $sRequest . " :Your request has been seen by syam"; echo $sResponse;. So please check your ASP code. Commented Jan 3, 2012 at 10:21

3 Answers 3

3

I believe the error is in your onreadystatechangedhandler. It will receive an event param, in which the target property points to the XHR-instance.

Try swapping it out with this:

xmlHttpRequest.onreadystatechange = function (event) { var xhr = event.target; if (xhr.readyState === 4 && xhr.status === 200) { document.getElementById("target").innerHTML = xhr.responseText } }; 
Sign up to request clarification or add additional context in comments.

1 Comment

change xhr.responseCode to xhr.status
1

send your request first

function sSignature(str) { xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.open("GET", "AjaxResponse.jsp?q=" + str, true); xmlHttpRequest.send(); xmlHttpRequest.onreadystatechange = function() { if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { document.getElementById("target").innerHTML = xmlHttpRequest.responseText; } } } 

Comments

-1

Mind that your code will not work in Microsoft Internet Explorer.

Secondly, modify one line of code to make it look better -

xhr.send() by xhr.send(null);

1 Comment

Why use xhr.send(null) instead of xhr.send()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.