homepage Welcome to WebmasterWorld Guest from 54.211.219.178
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
read an external file into a variable as a sting
Sarah Atkinson




msg:4181431
 8:48 pm on Aug 3, 2010 (gmt 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?

 

Sarah Atkinson




msg:4181464
 9:58 pm on Aug 3, 2010 (gmt 0)

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

var file=$.get(URL);
alert(file);

Sarah Atkinson




msg:4181480
 10:20 pm on Aug 3, 2010 (gmt 0)

i have tried this too and it comes back empty
$.ajax({
url: URL,
success: function(data){alert("Data Loaded: " + data); },
dataType:'html'
});

astupidname




msg:4181730
 11:44 am on Aug 4, 2010 (gmt 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'
});

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved