0

I have the following XML file which I am trying to parse ...

<?xml version="1.0" encoding="utf-8"?>

<root>
    <post>
        <heading>Test Post, Please Ignore</heading>
    </post>
</root>

And the JQuery I'm using alongside it ...

<script>
    $(document).ready(function(){
        $.ajax({
            type:"GET",
            url:"postList.xml",
            dataType:"xml",
            success:function(xml) {
                $(xml).find("post").each(function() {
                    var pTitle = $(this).find("heading").text();

                    $("#output").append("<p>" + pTitle + "</p>");
                });
            },error: function() {
                alert("An error occurred while processing XML file.");
            }
        });
    });
</script>

Every time I preview my site in broswer it throws an error (the one written into the JQuery). I have tried it in IE, Firefox and Chrome and have used W3C's XML Validator to make sure that that is all in check and yet it still doesn't work.
From what I can tell it should work and yet it doesn't. Instead it throws the error every time and my page is left with an empty <div id="output">. Any help would be much appreciated!

EDIT: I moved the XML file to the same folder as the html file and updated the url so that it has the correct address. It now appears to work in Firefox, but still throws and error in IE and Chrome ...

20
  • change the error handler to error: function (jqXhr, status, error) { alert(status + ':' + error + ':' + jqXhr.responseText) } and see the actual error that is thrown Commented Feb 19, 2014 at 2:15
  • Thanks, the error it throws in Chrome is error:: and error:Error:Access is denied. :undefined in IE I moved the XML file to the same folder as the page and it now works on Firefox, but nothing else ... Commented Feb 19, 2014 at 2:22
  • are you loading the page from a file: path? or do you have a web server from which you are serving the web page using http/s: protocol Commented Feb 19, 2014 at 2:24
  • whether the error is gone now? Commented Feb 19, 2014 at 2:25
  • I am currently testing it straight from my hard drive, so all files are local Commented Feb 19, 2014 at 2:26

1 Answer 1

1

As it turned out It was a problem with Same Origin Policy violation where the files was loaded from the local hard drive(file:// protocol).

Since the project is supposed to be deployed in a web server, the suggested fix is to use a local web server to host the files so that the same origin policy violations can be rectified.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.