ehfeng ehfeng - 2 years ago 104
Javascript Question

Use Javascript to retrieve HTML on same-domain

Say I have an xml document on a webserver ( On my main page, if I would like to retrieve that document as a string, how can I do this?

I tried xmlhttprequest - maybe I'm using it wrong? It returns it as "undefined." Help?

var xml_page = new XMLHttpRequest();"GET", "", true);
if (xml_page.readyState == 4 && xml_page.status == 200) {
var data = xml_page.responseText;

Answer Source

Your document.write fires before the data bas been fetched, to get your code work work mostly as-is, it'd have to be a synchronous request, like this:"GET", "", false);

However, it's better to keep it asynchronous (which eliminates document.write...also a good thing), like this:"GET", "", true);
xmlhttp.onreadystatechange = function() {
  if (xml_page.readyState == 4 && xml_page.status == 200) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download