Welcome to WebmasterWorld Guest from 54.159.250.110

Forum Moderators: open

Message Too Old, No Replies

read an external file into a variable as a sting

   
8:48 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



I have a path to a file located on another server, I want to load the contents into a variable as a sting
how do i do this?
9:58 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



i have tried to use get but that doesn't work
it just returns an object

var file=$.get(URL);
alert(file);
10:20 pm on Aug 3, 2010 (gmt 0)

10+ Year Member



i have tried this too and it comes back empty
$.ajax({
url: URL,
success: function(data){alert("Data Loaded: " + data); },
dataType:'html'
});
11:44 am on Aug 4, 2010 (gmt 0)

5+ Year Member



If you want to read the contents of the file within the javascript on your page, use php to retrieve the remote files contents:

relay.php file:

<?php
//>relay.php

$file = $_GET['file'];
if (is_string($file) && strlen($file)) {
$fc = @file_get_contents($file);
header('Content-type: text/html');
echo ($fc) ? $fc : 'ERROR LOADING FILE';
}

?>



Then access the relay.php file from ajax, sending it a 'file' GET parameter:

$.ajax({
url: 'relay.php?file='+ URL,
cache:false,
success: function(data){
alert("Data Loaded: " + data);
},
dataType:'html'
});