tags:

views:

35

answers:

1

Not sure if it's possible but how do I read a resource from a url using javascript without ajax?

for example, the following url is a static text file containing json encoded text

http://mysite.s3.amazonaws.com/jsonencodedcontent.txt

I'd like to use javascript to read the content from above link, read the json content into a javascript variable.

I can't use ajax because of cross site and I have no control over amazon S3 domain.

anyway to achieve this?

+2  A: 

Try @Ben's suggestion first. If for any reason that doesn't work in your case, here's two options I've both seen and used, which may or may not be available in your case (I'm providing two overly simplified examples just to clarify my points):

Create a server side resource that resides in your domain and retrieves and returns the cross site content for you:

<?php
die(file_get_contents('http://mysite.s3.amazonaws.com/jsonencodedcontent.txt'));

Use mod_rewrite (or something similar) to create a virtual resource in your domain that resolves to the remote content behind the scenes:

RewriteEngine On
RewriteRule ^jsonencodedcontext\.txt$ http://mysite.s3.amazonaws.com/jsonencodedcontent.txt [P]
Lauri Lehtinen
Thanks for the suggestion Lauri. the reason I use s3 is to reduce my server bandwidth load. But option 2 sounds like it, I'll give it a try
just tried option two. the problem is that when I use jQuery to issue an ajax call on http://mysite.com/jsonencodedcontext.txt, it doesn't follow 302 redirect that mod_rewrite issued.
My understanding is that the mod_rewrite should not issue a redirect, unless you use the [R] flag. The end result should be completely hidden from jQuery.
Lauri Lehtinen
I think with some tweaking your method should work for me. but I ended up with using jQuery's jsonp to achieve this. Thanks for the tips