views:

53

answers:

2

I have regular javascript code and am trying to utilize JQuery just to load and parse an xml file. I have the following:

function loadXml() {
  $(function() {
        $.ajax({
                type: "GET",
                url: "my_file.xml",
                dataType: "xml",
                success: parseXml

        });
    });

}

function parseXml() {
   alert("Parsing...");
}

When calling loadXml I cannot get the callback success function to execute. I am running this all locally and my_file.xml resides in the same directory as this javascript. Any ideas?

A: 

Most modern browsers will not allow the use of ajax to retrieve file:// based resources, even when the main document is run from the 'same' domain.

So in effect, you are running into the Same Origin Policy here.

Just set up a local http webserver and it should be no problem.

To explain the problem:

What if a document you downloaded into c:\users\foo\documents\CoolApp\ could read ANY file on the 'same' domain?

I guess you get my drift...

Sean Kinsey
hmm..thanks. Is there any way around this so that I can test without a webserver?
es11
Nope, only by lowering your security settings (if the browser allows that), and you really don't want to do that as a) your browser becomes unsafe and b) the environment you test in will not match that of others
Sean Kinsey
Incorrect Sean. Please test this locally yourself as it will work perfectly fine. What part of accessing a local file locally is invalidating the same origin policy?
lark
@lark: in what browser are you testing? Because I know plenty that will not do this, just search SO for similar questions
Sean Kinsey
I tested in latest versions of firefox and safari.
lark
@lark: Oh I have my doubts about that :)
Sean Kinsey
+1  A: 

Also why are you using the DOMReady shortcut function inside of another function, that doesn't make any sense.

Remove the $(function() { }); That might even fix your problem...

If anything should be set up like this:

jQuery(function($) {
    loadXML()
});

Full code would look like this (with the my_file.xml residing in the exact same directory):

<script src="http://code.jquery.com/jquery.js"&gt;&lt;/script&gt;
<script>
function loadXml() {
        $.ajax({
                type: "GET",
                url: "my_file.xml",
                dataType: "xml",
                success: parseXml

        });
}

function parseXml(data) {
   alert(data);
}


$(function() {
loadXml();
});
</script>
lark
Thanks I removed that, but it didnt fix the issue.
es11
Well I just used that exact code locally and it works fine (using file://). What else are you doing that you aren't showing us?
lark
Could you please show the entire code snippet you used to load the xml? I may have misunderstood the code you posted.
es11
es11: This has nothing to do with the pages load event, and who are you to say how his application should be set up when all you can see is a small piece of it? And the users problems are due to the SOP
Sean Kinsey
es11: The file I created was called test8.html and I used a generic xml file called my_file.xml to load it in. Worked fine.
lark
maybe sean was correct as I used the exact code you showed (and its doing nothing else) and its not working in either Chrome or IE
es11
That's perfectly plausible. I've never considered using IE for testing, but I'm surprised at Chrome.
lark