-1

I want to use an XML file for creating a sort of Dynamic website with jquery.

When testing on a local webserver i see via developer console on chrome that the xml file is not visible.

When opening the homepage in the local file (not using the webserver) I get the following error:

XMLHttpRequest cannot load file /// . Cross origin requests are only supported for protocol schemes: http, data, ...

I read XMLHttpRequest cannot load file. Cross origin requests are only supported for HTTP but since am using an IIS local server I already rules that out.

My code for reading the xml:

$.ajax({

    type: "GET",
    url: "data.xml",
    dataType: "xml",
    success: function(xml){
        var xmlDoc = $.parseXML(xml);
        var root = $(xmlDoc).find("root");
        alert(root);
    }

});

the project i have exists out of a few files:

index.html
code.js            ==> my own code
jquery.js          ==> jquery code
style.css
data.xml
Web.config         ==> I'm not entirely sure if i need this since i won't be using connections.
Website.sln        ==> I use a vs2013 express edition

This is my first project with jquery and though the adding and dropping of html content works perfectly, reading out an XML file keeps me at bay. I created a web.config file via visual studio express but i'm not entirely sure that i need this.

EDIT:

with the following changes I got it done:

firstly: replace the data.xml to a subdirectory (I don't know if it was this or not)

secondly: drop the parseXML function and go for a

$(this).find(name).each(function(){ //your code });

Eh voila, no back to programming...

Community
  • 1
  • 1
Schuere
  • 1,579
  • 19
  • 33
  • 1
    You can't access local files with javascript. You should use a local server. – Vincent Beltman Nov 21 '14 at 08:13
  • If you really want to do it like this, try making a regular Ajax call to a PHP script that reads your XML data and returns the contents. Then you can try to parse it. – Nick Nov 21 '14 at 08:16
  • @VincentBeltman i'm using a local IIS server while running via VS, what am I forgetting? – Schuere Nov 21 '14 at 08:21
  • 1
    I think your url isn't right, cause a XMLHttpRequest should go over http:// and over file///. It reads data.xml as a local file. – Vincent Beltman Nov 21 '14 at 10:33

1 Answers1

1

with the following changes I got it done:

firstly: replace the data.xml to a subdirectory (I don't know if it was this or not)

secondly: drop the parseXML function and go for a

$(this).find(name).each(function(){ //your code });

url then became = url: "../data/data.xml",

Schuere
  • 1,579
  • 19
  • 33