www.webdeveloper.com
Results 1 to 7 of 7

Thread: How to read text file from different domain

  1. #1
    Join Date
    Aug 2011
    Posts
    7

    How to read text file from different domain

    Hi guys,
    I need some advice.
    My situation is this:
    I have a router with OpenWrt(Linux distribution) installed and a web server configured in it. This router act as a client in a local network.
    I have created a simple web page on the Internet that enable the login to the openwrt web server.while I have to check whether the user, who request to login and the router are located in the same local network before I redirect the user. I have the IP of the router in the local network, which is always being updated remotely.
    I tried to do something like this:
    (The only way that worked so far for me..)
    Code:
    function ping(ip)
    {
     Image im = Image();
     im.src  = "http://" + ip + "/some_image.png";
     return (im.width>0 || im.height>0 )
    
    }
    Is there any way that I can read some text file from the router instead ??
    I tried loading a text file from the router into an iframe and then extracting the text value with JS from that iframe but for security reasons the browser does not allow it.
    As far as I know the only way to read a text file would be using ajax,
    but that is also disabled since the request is for another domain.

    By the way, for some reason my solution does not always work.
    Sometimes my function returns false, even when the image was loaded successfully in the remote page.(into a div for example)

    Anyway the solution must be in javascript since only client
    side language can reach to computer inside a home network when the only data available is the local IP address.

    Any help would be appreciated!

  2. #2
    Join Date
    Feb 2011
    Posts
    231
    Hi
    Maybe you can use ajax, by accessing a server side script, like php, that gets the file you want (with cUrl, or file_get_contents() ) and returns the content to ajax.

  3. #3
    Join Date
    Aug 2011
    Posts
    7

    ajax

    Thank you MarPlo for your advice but unfortunately that is not possible.
    I have already tried to do that.

  4. #4
    Join Date
    Feb 2011
    Posts
    231
    Sorry, I not know way is not possible. A PHP script can get the content of a page from other domain, and then to output it.
    But if you tried, you know better. It was an ideea.

  5. #5
    Join Date
    Aug 2011
    Posts
    7

    Ajax

    Hi MarPlo,
    To be more accurate it is possible but in my particular case it isn't.
    I will describe the issue to be more specific
    I have a local network with router and 2 computers.
    Computer #1 IP: 10.0.0.20
    Computer #2 IP: 10.0.0.27
    Notice that the above Ip addresses are local and valid only within the home network.
    My website is on the Internet, which means that php can not reach inside the home network, but javascript code runs from computer #1 browser and I need to read a text file which is located in computer #2.(This coumputer run a web server).
    I couldn't figure out how to do it.
    Due to browser security restrictions ajax request can not successfully retrieve data from a different domain or subdomain.

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    It's possible to import a js file from an other site... Transform your text files in javascript variables (var str="txtFileContent" and import the corresponding files...

  7. #7
    Join Date
    Aug 2011
    Posts
    7
    Thank you Julien,
    your solution did work.
    I solved it with Jquery:
    Code:
    $.getScript('http://'+IP+'/File.js',function(script){
        
      });
    For what I understand in javascript this and JSON-P,
    which basically works the same way = loading remote javascript fie
    are the only way to overcome the cross doamin obstacle.

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