views:

126

answers:

3

Hi All, Is it possible to get files from two different servers using HTML or javascript, Lets say File A is in Server A and File B is in Server B.Is it possible to access both the files and display them EDIT: pls post some code

+1  A: 

Using Javascript (since that's how you've tagged the question), you can't request the file since you'll fall foul of the cross domain AJAX limitation.

You would need to request the page using something like cURL, or urllib on the server side.

For example, with Python you could do something like:

import urllib2
page = urllib2.urlopen('http://www.google.com').read()
print(page)
John McCollum
can u post some dummy code
Wondering
+1  A: 

You can create a script on Server B that returns JSONP-representation of file specified in query string. Then dynamically (using JavaScript, i mean) create <script> tag like this:

<script type="text/javascript" src="http://server-b.com/get-file.php?file-b.txt"&gt;&lt;/script&gt;

to include it just like JavaScript file. After inclusion you'll get the content of File B specified in callback function argument.

aztek
+1  A: 

Since your questions asks how to achieve this using HTML or JavaScript and there are already a couple answers for JavaScript, I'll share my thoughts about how to do this in HTML.

This is probably a bit easier than using JS:

For HTML, there are two types of URL references you can use: Relative and Absolute. The type we're concerned with for multiple domains is absolute. You should be able to access a file on a different server (domain) using an absolute path. Say Server A's domain name is servera.com and the file file_a.htm is on the root web directory. The code you're looking for is:

<a href="http://servera.com/file_a.htm"&gt;File A on Server A</a>

Of course, you can display this in an inline frame if you don't want to redirect users from your page:

<iframe src="http://servera.com/file_a.htm"&gt;
  <p>Iframes are not supported by your browser. Please consider upgrading.</p>
</iframe>
T Pops