2

There may be a small error in my code. please advice me.

I want to call a URL and display the value in div on pageload.I wrote this code from SO but the responseText doesnt write the value in the div element's innerhtml

Code

<script type="text/javascript" >    
 var req ;
// Browser compatibility check          
if (window.XMLHttpRequest) {
   req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {

 try {
   req = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {

   try {
     req = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e) {}
 }

}


var req = new XMLHttpRequest();
req.open("GET", "www.example.com/Default.aspx?usrname=john",true);
req.onreadystatechange = function () {
    document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText;
}

req.send(null);
</script>
 <html>
 <head/>
 <body>
 <div id="divTxt"></div></body>
 </html>

The output I get is
My status :

PS: I want this to be done after pageload and The url returns a value "online" when called manually

EDIT This is the code I referred : code

Community
  • 1
  • 1
Coder323
  • 580
  • 6
  • 17

2 Answers2

0

req.onreadystatechange = function () { document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText; }

you have to check, if the request was successful:

if (req.readyState === 4) { // what should be done with the result }

finally, it has to look like this:

req.onreadystatechange = function () {
if (req.readyState === 4) {
document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText;
}
}

mightyplow
  • 519
  • 2
  • 6
  • 11
  • sorry for the bad markup. i'm not firm with the forum-syntax, yet ;) – mightyplow Jun 04 '11 at 07:44
  • @mightyplow , from where does this reqObj comes from? After I modified the code like you said, it shows nothing as output whereas previously it showed My status: – Coder323 Jun 04 '11 at 07:51
  • there was a mistake in my previous code. i changed it. the variable respObj is given to the eventHandler by the browser. i'm goin to test you're complete snippet right now and i'll give you back the correct one... – mightyplow Jun 04 '11 at 07:54
  • @mightyplow, I checked with your updated code but still it shows nothing as output – Coder323 Jun 04 '11 at 07:57
  • ah sorry. i did something wrong... you were completely right. forget what i did with the respObj stuff. it is req.readystate === 4 and req.responseText. sorry for the stolen time – mightyplow Jun 04 '11 at 08:08
  • @mightyplow, now it looks like my original output. i.e) My Status: , the reponseText doesnt show the output – Coder323 Jun 04 '11 at 08:11
  • how do you give back the result by the server? maybe you use "return" instead of "echo"? – mightyplow Jun 04 '11 at 08:18
  • hmm... i just saw, you're not using php. don't know how it works in your case. it seems like the return method of the server isn't right. – mightyplow Jun 04 '11 at 08:24
0

You cannot ajax a url from another domain unless it has implemented CORS

If you need to get data from somewhere which is not same origin you need to use JSONP

Also to debug, try calling the url from the locationbar to see if you receive valid data for your request

mplungjan
  • 169,008
  • 28
  • 173
  • 236