JavaScript中的XML调用无法正常工作

问题描述:

你好朋友,

我在使用javascript调用xml时遇到问题.
实际上我有一个html页面,我必须调用xml文件并绑定下拉列表.
当我放入服务器并访问它时,它的工作正常.
但是在通过应用程序本地调用时却无法正常工作.
在服务器中也在本地尝试调用其不起作用.

我尝试使用下面的代码片段,两者的结果相同,

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,

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;
}





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.

XMLHttpRequest仅在http://或https://URL上不起作用.即,您不能使用它来加载本地文件.如果要加载外部资源,而不是可以加载普通HTML标记(img,脚本,链接)的外部资源,则需要运行本地HTTP服务器对其进行测试.
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.


好,看看您的代码,我认为您已经合并了应该分开的逻辑.

1.使用ajax通过ReadFromXmlFile.aspx页面从服务器端获取文件content/xml.该页面将从xml文件读取,并将其返回给您的客户端.

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.

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;
}