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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why can't i recieve the external html code using xmlhttpreq...

Status
Not open for further replies.

djrichar

Programmer
Jun 3, 2005
7
US
hi there I am working on a webpage that needs to retrieve some data off of another web page. below is my code that i am using. My question is why is the varaible that stores the html code from the other site getting the value of null. I was hoping that it would give the me html code of the other page.

please help???

var req;
var cU = {
init: function() {
if (!document.getElementById) return;
if (!Sarissa) return;
//if (!cU.name) return;
loadXMLDoc(' },

addEvent: function(elm, evType, fn, useCapture) {
// cross-browser event handling for IE5+, NS6 and Mozilla
// By Scott Andrew
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
}
}

cU.addEvent(window, 'load', cU.init, false);


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

req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {

req.open("GET", url, true);
req.send();
req.onreadystatechange = processReqChange;
}
// }
// }
}


function getTables(dom) {

document.getElementById('texthtml').appendChild(document.createTextNode("HELP HELP!!!"));
//var alternative = .getElementsByTagName('body')[0];
document.getElementById('texthtml').appendChild(req.responseText);
}


function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
getTables(req.responseXML);
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}
 
From DevGuru:
...If the response was generated by an ASP page, and the MIME type was not correctly set to 'text/xml', responseXML will be empty.

If this is the case, try using responseText instead. Also, your argument for your getTables() function never gets used.

Adam
 
i tried that too. I still get null values.

Yes i know it doesn't get use b/c the code that used was working so i changed it to "req.responseText"
 
Code:
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
	     req = new XMLHttpRequest();	     
	     req.open("GET", url, true);
	     req.send(null);
	     req.onreadystatechange = processReqChange;
	     
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {	     
	     req = new ActiveXObject("Microsoft.XMLHTTP");
	     if (req) {	       
	        req.open("GET", url, true);
	        req.send();
	        req.onreadystatechange = processReqChange;
	     }	  
          }
    }
}

    
function getTables(dom) {
    
    document.getElementById('texthtml').appendChild(document.createTextNode("HELP HELP!!!"));
    var alternative = dom.getElementsByTagName('table');
    for(var i=0; i<alternative.length; i++) {
    	document.getElementById('texthtml').appendChild(alternative[i]);
    }
}

there's the original code that was changed. but i still get null values
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top