简体   繁体   中英

Importing XML in HTML with javascript

I am trying to import a XML-file into a HTML document with Javascript.

It works fine for Firefox, but it doesn't work for IE-10 and Chrome.

My script:

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","xml_input.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

I think they changed the XMLHttpRequest again but I can't find it's replacement anywhere. I'm hoping someone on this site can help me.

Are you using a server of some kind? If you're testing off of a local address, eg file://whatever/your/thing/is you won't be able to make any HTTP requests by default unless you're in Firefox or Safari.

If you are using a server, do you get any errors in your console? Can you see the request being attempted in the network tab?

How about importing jquery into your project and using its wonderful $.ajax function. It will figure out the stuff under the covers (XMLHTTP) and you won't have to worry about this anymore. You want this to be abstracted away!.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM