Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xmlhttprequest - arghhh 1

Status
Not open for further replies.

Trebor100

Programmer
Joined
Mar 14, 2006
Messages
90
Location
GB
Hi all,
Trying my hand and javascript but I am unable to get the xmlhttprequest object to work properly. The code im using is as follows and the XML is included at the bottom of this message. Any help (even if it's "your an idiot add ..." level) would be much appreciated :)

<html>
<head>
<script type="text/javascript">
var req;

function loadXMLDoc(url)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
}

function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
alert("wahoo");
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
}

</script>
</head>

<body onload="loadXMLDoc('\TUTOR\XML\subset.xml')">
<h2>Using the HttpRequest Object</h2>

</body>
</html>

and the XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<SUBSET>
<ENTRY>
<REF>1</REF>
<TITLE>Global Course</TITLE>
</ENTRY>
<ENTRY>
<REF>2</REF>
<TITLE>Seminars</TITLE>
</ENTRY>
<ENTRY>
<REF>3</REF>
<TITLE>Conference</TITLE>
</ENTRY>
<ENTRY>
<REF>4</REF>
<TITLE>ICT</TITLE>
</ENTRY>
<ENTRY>
<REF>5</REF>
<TITLE>HR And Payroll</TITLE>
</ENTRY>
<ENTRY>
<REF>6</REF>
<TITLE>2010</TITLE>
</ENTRY>
</SUBSET>

Cheers all,
Rob
 
I have not worked with XMLHTTPRequest myself but here is some sample code I found on another site for reading a file from a server.

Code:
<script type='text/javascript'>
<!--
function include(lyr,url)
{
   if (document.all)
   {
      // IE version
      try {
      var xml = new ActiveXObject("Microsoft.XMLHTTP");
      xml.Open( "GET", url, false );
      xml.Send()
      document.getElementById(lyr).innerHTML=xml.responseText;
       }
      catch (e) {
      var xml = new ActiveXObject("MSXML2.XMLHTTP.4.0");
      xml.Open( "GET", url, false );
      xml.Send()
      document.getElementById(lyr).innerHTML=xml.responseText;
      }
   }
   else
   {
      // Mozilla/Netscrap 6+ version
            var xml=new XMLHttpRequest();
            xml.open("GET",url,false);
            xml.send(null);
            document.getElementById(lyr).innerHTML=xml.responseText;
   }
}
//-->
</script>

Of course this version is using ActiveX for IE browsers so you might get ActiveX warnings. It might give you ideas about your own code though.


At my age I still learn something new every day, but I forget two others.
 
><body onload="loadXMLDoc('\TUTOR\XML\subset.xml')">
[tt]<body onload="loadXMLDoc('\[red]\[/red]TUTOR\[red]\[/red]XML\[red]\[/red]subset.xml')">[/tt]
 
Or may be you mean this as well.
[tt]<body onload="loadXMLDoc('[red]/[/red]TUTOR[red]/[/red]XML[red]/[/red]subset.xml')">[/tt]
 
Cheers tsuji - was pulling my hair out (and with a number 1 all over there isnt much to go at).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top