0

I need to load a xml file from my pc in ajax, but the script it's not working at it's current state(no clue why). Here is what i have:

$.ajax({
    type: "GET",
    url : 'file:///C:/xampp/htdocs/files/license/index_skin.xml',
    dataType: "text",
    success : function (data) {
        $("#txarea").val(data);
    }
});
Radu Dascălu
  • 318
  • 4
  • 15
  • have you checked the network tab of the dev tools to see what's going on? I'd guess it's the path `url : 'C:/xampp/htdocs/files/license/index_skin.xml',`. Have you tried `url : '/license/index_skin.xml',` instead? – j08691 Nov 25 '14 at 22:15
  • @j08691 the thing is that i need full path to the file, it works if i write just '/license/index_skin.xml', also, i tried 'file:///C:/xampp/htdocs/files/license/index_skin.xml' in chrome and it worked. I don't know what is the issue. – Radu Dascălu Nov 25 '14 at 22:19

1 Answers1

0

There are multiple issues in play here:

  1. File URLs don't use the exact window path names c:\xxx. They have to be actual file URLs.
  2. All browsers prevent "cross origin" ajax requests. A cross origin request is any request where the first part of the URL (protocol, port, domain) are not the same as that of the page that is loaded. This may explain why it works with a relative path, but not an absolute path. Perhaps the first part of the URL you are trying to use is different than the first part of the URL you loaded the page from. What is the page URL when you are trying this?
  3. Some browsers won't let you load any file from your local disk (for security reasons) even if the HTML file is local and even if the origin is the same.
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Currently i'm working on localhost, so page URL is http://localhost. Also, i know that chrome is using 'fakepath' when you try to get a value from an 'input file' form, thats why im trying to get full path to the file on my pc in other way. – Radu Dascălu Nov 25 '14 at 22:26
  • @RaduDascălu - I'd suggest you read [this page](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) on same origin requests. It probably explains why you can't use a `file://` URL if your web page is loaded from `localhost`. – jfriend00 Nov 25 '14 at 22:32
  • so there is really no way to get access to a file on my pc with an ajax script on my localhost? – Radu Dascălu Nov 25 '14 at 22:36
  • @RaduDascălu - assuming you actually have a web server running on localhost, you can create a route that your server responds to and have the server get the file and return it as the response to the route. You cannot access arbitrary file system URLs directly from the browser. So, you could modify your server so that requesting this URL from your server `http://localhost/license/index_skin.xml` (or whatever URL makes the most sense for you) actually returns the contents of `C:/xampp/htdocs/files/license/index_skin.xml`. – jfriend00 Nov 25 '14 at 23:04