tags:

views:

72

answers:

4

I am using this code to load the content of a text file:

function go(){  
        //var myFile = "c:/BMCclient.log";  
        var myFile = "abc.html";
        $.get( myFile, function(data) {
        alert ( data );
            $('#_content').html(data);
            alert('Load was performed.');
            });

    }   

When myFile = "abc.html"; this method executes successfully, but when I use myFile = "c:/BMCclient.log"; then it throws an exception:

uncaught exception: [Exception... "Component returned failure code: 0x805e000a [nsIXMLHttpRequest.open]" nsresult: "0x805e000a (<unknown>)" location: "JS frame :: file:///C:/PIC/batch/personal/web/js/jquery/jquery-1.4.2.js :: anonymous :: line 5113" data: no]

Why it is happening?

+1  A: 

Ajax doesn't play well with the local file system, you'd have better luck if you setup a web server, and access the site using http://localhost.
You're in luck HTML files work for you - most browsers don't allow that either.

Kobi
+4  A: 

This is almost certainly due to a specific aspect of the Same Origin Policy for file URLs (emphasis mine).

Starting in Gecko 1.9, files are allowed to read only certain other files. Specifically, a file can read another file only if the parent directory of the originating file is an ancestor directory of the target file. Directories cannot be loaded this way, however.

For example, if you have a file foo.html which accesses another file, bar.html, the load will succeed only if bar.html is either in the same directory as foo.html or in a directory contained within the same directory as foo.html.

Pekka
A: 

This is happening because JavaScript cannot access files from your computer's file system. If that was possible then JS scripts could steal your file's data without you even knowing so it's designed to work like that.

RaYell
Not entirely true: He is making the request from the local filesystem.
Pekka
Still, you can't access the file by drive letter.
RaYell
A: 

You can't load files in javascript by addressing them using your filesystem. $.get is a HTTP GET request, which will talk to a server (e.g. on localhost).

The MYYN
Are you sure? I *think* this is possible. Can't test right how.
Pekka