Welcome to WebmasterWorld Guest from 54.166.179.165

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)

Preferred Member

10+ Year Member

joined:Apr 29, 2003
posts:424
votes: 0


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)

Preferred Member

10+ Year Member

joined:Apr 29, 2003
posts:424
votes: 0


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)

Preferred Member

10+ Year Member

joined:Apr 29, 2003
posts:424
votes: 0


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)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


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'
});
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members