www.webdeveloper.com
Results 1 to 9 of 9

Thread: Read Text File and create dynamic html table - "Access is denied"

  1. #1
    Join Date
    Apr 2013
    Posts
    4

    Read Text File and create dynamic html table - "Access is denied"

    I would like to read txt file and create dynamic html table, below coding. But It shows "Access is denied". It only allowed me to load the "test.txt" in local server. But I would like to store the test.txt in another file server. Pls advise. Many Thanks.

    <html>
    <head>
    <title>Read File</title>
    </head>

    <body>
    <script>
    function display_file()
    {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", "file:\\FileServer/temp/Test.txt", true);

    httpRequest.send(null);
    httpRequest.onreadystatechange = function()
    {

    if(this.readyState == 4 && this.status == 200)
    {
    var fileContent = this.responseText;
    var fileArray = fileContent.split('\n');
    var n = fileArray.length;

    var table = '<TABLE WIDTH="100%" CELLSPACING=0><TR>';

    for (var i=0; i<fileArray.length; i++) {
    var rowArray = fileArray[i].split(',');
    var row = '';
    for (var j=0; j<rowArray.length; j++) {
    row += '<TD><FONT SIZE=2> <P>' + rowArray[j] + '</FONT></TD>';
    }
    table += '<TR VALIGN=TOP>' + row + '</TR>';
    }
    table += '</table>';
    document.getElementById('p1').innerHTML = table;
    }
    }
    </script>
    <body onload="display_file()" /><br />
    <p id="p1"></p>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    Make sure the permissions allow you to read the text file on the server.

    I'm not sure JS can read a text file from a different domain. Might require script to be in same domain as text file. (Something to check!)

  3. #3
    Join Date
    Apr 2013
    Posts
    4
    I already granted the right the everyone "READ". But I'm not sure whether Ajax support to call another file server to receive the txt file. Because I'm able to load the file if the txt file stored in local server. Pls Advise. Thanks.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    I'm about 99% sure that JS can not access AJAX code from a different domain. I believe it needs to be in the same domain.
    I'll leave 1% doubt for other forum members to correct me.

  5. #5
    Join Date
    Apr 2013
    Posts
    4
    Currently all my coding under same company which same domain, just different server. So I'm not sure whether it still cannot connect to others server with same domain. Thanks.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Arrow

    Quote Originally Posted by xostar1 View Post
    Currently all my coding under same company which same domain, just different server. So I'm not sure whether it still cannot connect to others server with same domain. Thanks.
    That's above my pay grade of knowledge
    Hopefully, some other forum members will know more than me.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    add CORS headers to your server with the text file and it can work from any domain.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    Quote Originally Posted by rnd me View Post
    add CORS headers to your server with the text file and it can work from any domain.
    Sorry for my ignorance ... still trying to learn:

    Is this a change to the server
    OR
    is it a <meta ...> tag inclusion?

    And,
    1. does it go into the text file to be read
    2. or the JS extension file file somewhere
    3. or the HTML file in the <head> section?

    Could you provide an example?

  9. #9
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by JMRKER View Post
    Sorry for my ignorance ... still trying to learn:

    Is this a change to the server
    OR
    is it a <meta ...> tag inclusion?

    And,
    1. does it go into the text file to be read
    2. or the JS extension file file somewhere
    3. or the HTML file in the <head> section?

    Could you provide an example?
    it's an HTTP header or headers that allow such ajax requests.
    if memory servers me correct, if the content-type header is "text/plain" or "text/html", you can send just the "Access-Control-Allow-Origin: *" header.
    if you ship a different mimetype or use custom HTTP headers, then you need to add additional CORS headers to opt-in. that advanced usage is likely not needed for the simple purpose of fetch a text file.

    see https://developer.mozilla.org/en-US/...s_control_CORS for full details.


    so, view the text file in chrome, press F11, goto network tab, reload if needed, and click the only http request, the text file to inspect it's headers.
    some hosts won't let you modify the headers of static files, either on-purpose or just because there's no button or control to do so.
    in that case, you can use a simple php/asp wrapper to load the file named in a query param and echo it back out after adding the ACAO=* CORS header.
    of course, if you're going to do all that you can json encode it and append a callback to it for jsonp usage from anywhere. they both work...

    something like

    Code:
     echo "callBack(" . json_encode(  file_get_contents("data.txt")  ) . ")";
    Last edited by rnd me; 04-04-2013 at 03:57 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles