-->

Reading xml with out using local/webserver

2019-04-02 22:25发布

问题:

I am new to javascript.I am trying to read an xml file from application folder, which is located in the hard disk. "Cross origin requests are only supported for HTTP." this is the error that is being thrown.

If I try to access the same xml file via local server/web server it works fine. Since i am trying to develop offline application. How do i make it work locally.

Thanks,

回答1:

I can imagine a few reasons why you want to develop an offline application :

If you want an actual online application that might also work offline (with sync stuff in mind, or not) look at HTML5 : http://diveintohtml5.info/offline.html

Now, if you think "I have no server-side logic so I do not need a server, but I still dig having a javascript application with some html UI", and clearly we nearly-all thought at one point seeing what amazing UI capabalities bring smart APIs in a recent web navigator, all running locally and absolutely portable. "Well so I just need a bunch of javascript files, some html and css, I give it whatever xml/json/csv data to it that is somewhere on my enterprise network and here I go without having to either write down some crappy VBA within a huge excel sheet, or impact my IT/IS with web servers, and firewalls and such I can deploy powerful data viewers and indicators and such."

I mean I did say these things to me. And I did many times despise navigators behave just fine until I want to get the data and then complain just to fetch those damn data !

But I do have to admit that web navigators must first be trustful, and to be so, have to commit on only browsing consistent web-apps (safe logic implemented by safe behaviors on safe data), which means setting somewhere a sandbox with boundaries. Letting the app just be "whatever is on my computer" and at the same time being connected to www running within a navigator is just too open to be reliable.

If you want to have the html/css/javascript power of a navigator just use a web navigator engine (such as webkit.) What matter is that at the end it will be your own application, and you will be able to do whatever you want to do, at the expense of having to explain your IS/IT team that is harmless, because you have to.

Or if you make the effort to recognize that data provider is already a server responsibility go client server, with plenty of alternatives at hand :

  • you could have the usual Apache (some debugging all-in-one packages such as wamp works just fine within minutes)
  • you could move to a full-stack web-server API (such as Play!) to run background and use your preferred navigator on top of it (allowing you to go web at some point),
  • or use such combinations as nodejs and webkit or else to have all-in-one (such as https://github.com/rogerwang/node-webkit )

I went probably too far compared to your actual need : make that damn code work. And for that there are many answered questions on stack overflow about local file "Cross origin requests are only supported for HTTP" stuff.

But I had to make my point, this time. Hope it is not completely off track ;)



回答2:

This is probably due to the file you are trying to access is out of sandbox environment of your application.

The file which you want to parse should be residing at a location which is accessible to your app. User profile/Temp location (Documents/app data) of Desktop is such location. you cannot access entire file structure of user machine.

http://blogs.adobe.com/asset/tag/sandbox

https://developer.apple.com/library/mac/documentation/security/conceptual/AppSandboxDesignGuide/DesigningYourSandbox/DesigningYourSandbox.html