Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

ajax - Load xml file in a directory using javascript

I want to load a xml file using the javascript. I use the following code to load the xml file. The below coding loads the xml file when it is in the same folder.

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",'dineshkani.xml',false);
            xmlhttp.send();
            xmlDocument=xmlhttp.responseText;
            alert("loaded");

But i want to load the xml file in the particular location eg. c:/xml/dineshkani.xml

If i use the coding xmlhttp.open("GET",'c:/xml/dineshkani.xml',false); like this it wont load the xml file. Is there a way to load the xml file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Despite its name, XMLHttpRequest can be used for non-HTTP requests.
The following should work

xmlhttp.open("GET",'file:///C:/xml/dineshkani.xml',false);

The result status is 0 for success instead of 200. This is because the file and ftp schemes do not use HTTP result codes.

EDIT: However, some browsers, including Google Chrome disable this by default. It has to be enabled by starting Chrome with --allow-file-access. So if you are looking for a cross-browser solution, you should put the XML in your server directory.

HTML5 file api does not let you access the entire filesystem you get a sandboxed directory to work with. Link

Reference: MDN Page


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...