PDA

View Full Version : problem with my ajax


nikku
02-19-2008, 07:07 AM
hello everyone,

I have a ajax code which is showing an error in ie.
It is not able to read the xml file



function loadxml(url)
{


req = false;

var moz = (typeof document.implementation != 'undefined') && (typeof
document.implementation.createDocument != 'undefined');

var ie = (typeof window.ActiveXObject != 'undefined');

if (moz){ xmlDoc = document.implementation.createDocument("", "", null) ;}


// branch for native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
try {
req = new XMLHttpRequest();
if(req.overrideMimeType){

req.overrideMimeType('text/xml');
}

} catch(e) {
req = false;
}

// branch for IE/Windows ActiveX version
} else if(window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}

if(req){
req.open("GET", url, true);
req.onreadystatechange = stateChanged;
req.setRequestHeader("Content-Type","text/xml");
req.send(null);
}
}
function stateChanged()
{

if (req.readyState==4 && req.status==0)
{
xmlDoc=req.responseXML.documentElement;}
alert(xmlDoc); //alert1
}



My xml file is in the same directory as my above code.

My xml file opens properly in ie,firefox,and microsoft word.

The problem is alert1 gives me a null value.

Any suggestions will be of great help.

Thanks
Cheers!!!

shyam
02-19-2008, 12:04 PM
&

function stateChanged()
{

if (req.readyState==4 && req.status==0)
{
xmlDoc=req.responseXML.documentElement;}
alert(xmlDoc); //alert1
}



you should check for status 200 which is the OK status code for HTTP

nikku
02-20-2008, 05:52 AM
&
you should check for status 200 which is the OK status code for HTTP
Thanks shyam,

My xml file is in the same directory as my script.

I read that if you have a local xml file then the status should be 0 and if you are getting the file froma server then the status should be 200.

Can you also suggest why this runs in mozilla and not on ie.

Cheers!!!

Isaak
02-20-2008, 10:55 PM
Why don't you use a framework for AJAX such as xajax (http://xajaxproject.org/) (AJAX-only framework) or jQuery (http://jquery.com) (complete framework)? It will save you a lot of time and by reading the code will learn from your previous mistakes and even learn new tricks.