Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello frieds,

I have an issue for calling xml in javascript.
actually i have a html page and i have to call a xml file and bind dropdown.
while i am putting in server and acess it, its working fine.
but while calling through locally through application its not working.
in server also locally try to call its not working.

i tried with below code snippet, both have same result,
C#
function loadXMLDoc(XMLname) {
    var xmlDoc;
    if (window.XMLHttpRequest) {
        xmlDoc = new window.XMLHttpRequest();
        xmlDoc.open("GET", XMLname, false);
        xmlDoc.send(null);
        return xmlDoc.responseXML;
    }
    // IE 5 and IE 6
    else if (ActiveXObject("Microsoft.XMLDOM")) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.load(XMLname);
        return xmlDoc;
    }
    alert("Error loading document!");
    return null;
}


C#
function loadXMLDoc(XMLname) {
    if (window.DOMParser)
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(XMLname, "text/xml");
    }
    else
    // Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(XMLname);
    }
    return xmlDoc;
}


dont give optimize anser ur path is not correct.
Posted
Updated 13-Sep-11 0:55am
v2

XMLHttpRequest doesn't work except on http:// or https:// URLs. I.e., you cannot use it to load a local file. If you want to load an external resource, and not one that can be loaded with a normal HTML tag (img, script, link), then you will need to run a local HTTP server to test it.
 
Share this answer
 
Comments
Akshaya Dash 13-Sep-11 7:44am    
thanks,
u are absolutely correct.
for that i added this html page in one of my project and run this page, its working after that.

thanks for ur valuable suggestion.
Ok, looking at your code i think you have combined logic that is supposed to be separated.

1. Use ajax to get the file contents/xml from server-side using a ReadFromXmlFile.aspx page. This page will read from the xml file and return this to your client.

JavaScript
function GetXmlData()
{
  var xmlhttp;
  if ( window.XMLHttpRequest )
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest( );
  }
  else
  {// code for IE6, IE5
    xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }

  xmlhttp.onreadystatechange=function( )
  {
    if ( xmlhttp.readyState==4 && xmlhttp.status==200 )
    {
      var xDoc = LoadXml( xmlhttp.responseText );
      //read from the xDoc now
    }
  }
    xmlhttp.open( "GET","ReadFromXmlFile.aspx",true );
    xmlhttp.send( );
  }
}

function LoadXml(xmlData)
{
   if (window.DOMParser)
   {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(xmlData,"text/xml");
   }
   else
   // Internet Explorer
   {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlData);
   }
   return xmlDoc;
}
 
Share this answer
 
Comments
Akshaya Dash 13-Sep-11 7:49am    
Thanks 4 ur effort.
but what BobJanova suggested in solution-1 is the main cause 4 this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900