0

I am trying to access a file called "slider1.xml" and access information to post onto my web page. I am using Jquery, and right now I can't seem to get it to work. I think the issue may be with how I am calling the function.

slider1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<note1>Here is a note</note1>

main.js:

$(document).ready(function() {
    function loadXMLDoc(url){
        var xmlhttp;
        var txt, x, xx, i;
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                x = xmlhttp.responseXML.documentElement.getElementsByTagName("note1").nodeValue;
                alert(x);
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }

    loadXMLDoc("slider1.xml"); // this line is causing an error
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Just specify the complete url to the document... – Dimi Takis Jun 23 '15 at 12:53
  • What does the console show? – kosmos Jun 23 '15 at 12:54
  • XMLHttpRequest cannot load file:///home/kevin/Desktop/7cityevents/js/slider1.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.main.js:64 loadXMLDocmain.js:67 (anonymous function)jquery.js:3148 m.Callbacks.jjquery.js:3260 m.Callbacks.k.fireWithjquery.js:3472 m.extend.readyjquery.js:3503 J main.js:64 Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///home/kevin/Desktop/7cityevents/js/slider1.xml'.main.js:64 loadXMLDocmain.js:67 (anonymous function)j – Kevin McKeown Jun 23 '15 at 12:55
  • 1
    What is unclear about the error message? – Quentin Jun 23 '15 at 12:56
  • I don't understand what Cross origin requests are, the part I am taking note of is "Failed to execute 'send' on 'XMLHttpRequest' – Kevin McKeown Jun 23 '15 at 12:58
  • 1
    https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy – Quentin Jun 23 '15 at 12:58
  • You cannot make an ajax request to your filesystem, unless you have a built in webserver to handle the GET / POST requests. The lack of http headers in the response, makes it look like a cross origin request. You can circumvent this by hosting a small development server on your machine. That is elegantly shown in the answers to the duplicate question – Henrik Jun 23 '15 at 13:02
  • I think it is because I am passing an id into the url, ex: "file:///home/kevin/Desktop/7cityevents/index.html?cityid=Atlanta", but I am trying to access "file:///home/kevin/Desktop/7cityevents/js/slider1.xml" so that error doesn't make sense to me. – Kevin McKeown Jun 23 '15 at 13:03
  • you cannot (due to security measures) access the local filesystem via the "file://" protocol using an ajax request. instead start a local webserver. try (in a terminal) navigating to the folder with the website, typing this in a console: ` python -m SimpleHTTPServer ` for v. 2.x of python, or `python3 -m http.server` for python3.. then you can request the vile via http://localhost:8000/filename – Henrik Jun 23 '15 at 13:06
  • Found a Solution: http://stackoverflow.com/questions/20041656/xmlhttprequest-cannot-load-file-cross-origin-requests-are-only-supported-for-ht – Kevin McKeown Jun 23 '15 at 13:08

0 Answers0