0

I'm trying to load external xml using the following code but it is not working

$( document ).load( "data.xml", function(  response, status, xhr ) {        
    console.log( xhr.status + " " + xhr.statusText );
  });

I have both data.xml and js file in same folder.

In chrome it returns 404 error.

In FF it retuns 0 [Exception... "Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)".

I couldn't understand why this happens? Please shed some light on this issue.

Updates: I gave a shot using $.get() as mentioned below but still no success.

Meanwhile I also gave a try using pure js like below

function loadXMLDoc(dname) {
    if (window.XMLHttpRequest)    {
      xhttp=new XMLHttpRequest();
      }
    else {
      xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",dname,false);
    xhttp.send();
    return xhttp.responseXML;
}
    xmlDoc=loadXMLDoc("data.xml");
    console.log(xmlDoc);

Still facing errors.

Error in FF: NS_ERROR_DOM_BAD_URI: Access to restricted URI denied [Break On This Error]

xhttp.send();

and

Error in chrome: XMLHttpRequest cannot load file:///C:/Users/admin/Desktop/public_html%281%29/public_html/data.xml. Cross origin requests are only supported for HTTP. xml.js:13 Uncaught NetworkError: A network error occurred.

Updates: I found this question useful, but is there any way to solve this problem?

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • `$( document ).load( "data.xml"` doesn't make any sense. – Kevin B Nov 08 '13 at 18:24
  • First, you almost certainly want to use `.get()` and not `.load()` here. – Pointy Nov 08 '13 at 18:24
  • @KevinB Then do I need to use any element? for example `$('#someid').load(..` please correct me if I meant wrong? – Praveen Nov 08 '13 at 18:26
  • @Pointy I tried using `.get()` but it is not logging console messages. – Praveen Nov 08 '13 at 18:30
  • I hope you're using a local webserver to test this. Looking at your console error outputs I see you're trying to get a file from the `file://` protocol when it should be a relative path from the root folder which then uses `http://` protocol. – Tim Vermaelen Nov 08 '13 at 19:18
  • @TimVermaelen Just now figured out the problem, please have a look at [this](http://stackoverflow.com/a/19866904/1671639) – Praveen Nov 08 '13 at 19:21

2 Answers2

1

Maybe this is what you are looking for....

$(document).ready(function(){
    $.ajax({
        url: 'data.xml',
        dataType: 'xml',
        success: function(response, status, xhr){
           console.log( xhr.status + " " + xhr.statusText );
        }
     });
});

UPDATE

Read this post

Community
  • 1
  • 1
zgood
  • 12,181
  • 2
  • 25
  • 26
  • I tried it but it is not working. I'm not getting any consoles messages. – Praveen Nov 08 '13 at 18:29
  • Nope, this is not working. Same behavior but to see for error I tried `$.ajax({ url: 'data.xml', dataType: 'xml', success: function(response, status, xhr){ console.log( xhr.status + " " + xhr.statusText ); }, error: function (response, status, xhr){ console.log( xhr.status + " " + xhr.statusText ); } });` Now it logs `undefined undefined` means still error. – Praveen Nov 08 '13 at 18:36
  • @PraveenJeganathan Doing it the way you're currently doing it doesn't work either does? the way in this answer is far more correct than what you're currently doing. You should use this answer and add an error callback, then console.log the three arguments from the error callback. Most likely the xml is invalid.' – Kevin B Nov 08 '13 at 18:37
  • The third parameter of error: is not an xhr, it's a string. therefore of course you're getting undefined. Log it whole. – Kevin B Nov 08 '13 at 18:37
  • @KevinB I tried whole like `error: function (response, status, err){ console.log( err ); }` but only empty log prints. – Praveen Nov 08 '13 at 18:39
  • Make sure your `xml` is valid by running it through a validator like [this one](http://www.xmlvalidation.com/) – zgood Nov 08 '13 at 18:39
  • @PraveenJeganathan that's ok, just means jQuery didn't produce an error reason. Next log the status, and the response.responseText – Kevin B Nov 08 '13 at 18:41
  • @KevinB Thanks for giving me clear understanding, but it both returns `"error" and status: 404` – Praveen Nov 08 '13 at 18:44
  • @zgood I validated the xml it returns `No errors were found `. That's a good point, now I tried with whole system path it returns `statusText: "NetworkError: A network error occurred."` – Praveen Nov 08 '13 at 18:45
  • Move your `data.xml` file to your root from your `js` folder. Or change your `ajax` `url` parameter to `js/data.xml` or whatever your `js` folder is called and located – zgood Nov 08 '13 at 18:48
  • @zgood I already have like that. This has be mentioned in the question. – Praveen Nov 08 '13 at 18:50
  • Are you executing your javascript from a page or is it in a separate `js` file? – zgood Nov 08 '13 at 18:52
  • if you are gettign 404, then the path to your xml is wrong. there is no other reason to get a 404. – Kevin B Nov 08 '13 at 18:53
  • @KevinB I even tried giving the full path but [no success](http://stackoverflow.com/questions/19865938/unable-to-load-xml-from-external-file-using-jquery#comment29549162_19866002) I have also updated my question, please have a look. – Praveen Nov 08 '13 at 18:56
  • I have updated my answer with a post that may clarify your issue – zgood Nov 08 '13 at 19:02
  • @zgood +1 Thanks for you effort, I have [resolved](http://stackoverflow.com/a/19866904/1671639) the issue. Feeling of being so stupid. Thanks anyway ;) – Praveen Nov 08 '13 at 19:23
  • No problem. We have all been there ;) – zgood Nov 08 '13 at 19:25
1

After a long struggle and with the help of community I figured out the issue.

The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin.

Means this is not possible with the system file, so with the help of this answer, I used WAMPServer to run my script and it worked like a charm.

 $.get("http://localhost/public_html(1)/public_html/xml/data.xml",
                                     function(  response, status, xhr ) {        
        console.log( response );
    });

Thank you!

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164