开发者

How to read xml using javascript

开发者 https://www.devze.com 2023-04-03 12:50 出处:网络
Is it possible to read xml files locally from an html page using javascript without using any local server? 开发者_运维知识库

Is it possible to read xml files locally from an html page using javascript without using any local server? 开发者_运维知识库

Right now I am able to load xml using jquery.ajax function but it only loads xml files from server. What I need to do is to load xml from local file system. when i try to do it I get an error from chrome?

XMLHttpRequest cannot load file:///C:/Simplified.xml.

Cross origin requests are only supported for HTTP.


It's possible using some HTML5 API's. Checkout the FileReader API. Here's a working example. Tested in Chrome and Firefox. Does not work as of Safari 5.1.

// create a new FileReader object
var reader = new FileReader();
// setup an onload callback. This function
// will be called when the file has been 
// read/loaded in memory.
reader.onload = function(event) {
    console.log(reader.result);
};

// everything is set up. go read some files!
reader.readAsText(someFileInputField.files[0]);


You can run a server on your desktop. It is very very simple to set up an http server on almost any platform. They even come on a memorystick

That said,

Using this Google search I found Google Chrome --allow-file-access-from-files disabled for Chrome Beta 8 and possibly: Circumventing Chrome Access-control-allow-origin on the local file system?

0

精彩评论

暂无评论...
验证码 换一张
取 消