views:

179

answers:

5

I have looked everywhere and surprisingly can't find a good solution to this! I've got the following code that is supposed to read a text file and display it's contents. But it's not reading, for some reason. Am I doing something wrong?

FTR, I can't use PHP for this. It's gotta be Javascript.

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://www.mysite.com/todaysTrivia.txt", true);
txtFile.send(null);
txtFile.onreadystatechange = function() { 
 if (txtFile.readyState == 4) {  // Makes sure the document is ready to parse.
  alert(txtFile.responseText+" - "+txtFile.status);  
     //if (txtFile.status === 200) {  // Makes sure it's found the file.         
        var doc = document.getElementById("Trivia-Widget");
        if (doc) {            
         doc.innerHTML = txtFile.responseText ;   
   }
    //}
 }
 txtFile.send(null);
}

Any good ideas what I'm doing wrong? It just keeps givimg me a zero status.

EDIT: I guess it would be a good idea to explain why I need this code. It's basically a widget that other folks can put on their own websites that grabs a line of text from my website and displays it on theirs. The problem is that it really can't be server-side since I've got zero control over everyone else's sites that use this.

+7  A: 

If this is cross domain, you won't be able to do this with an xmlhttprequest due to the same origin policy.

Alex Sexton
Yes, it IS cross-domain
Cyprus106
+1  A: 

Your problem could be with the fact that you can only request XML data from the same domain via Javascript. This is the biggest issue with AJAX calls - if the text file is on another server, you can't get it via AJAX. If it's on the same server, make your request using a relative URL (no http://).


EDIT

Now that I know what you're trying to accomplish ... my recommendation would be to use an iFrame. Build the system on your server using server-side code and allow remote sites to embed an iFrame to display the output on their own sites. NetworkedBlogs uses this for displaying Facebook features on remote sites. iGoogle uses it extensively with their various Apps and Gadgets. It's a fairly tried-and-true method.

The advantage of using an iFrame is that you'll still have control over most of the content of the widget, but you can give end-users control over the styling (just have your iFrame application accept arguments via query variables to change colors, positions, and sizes).

EAMann
I would say dynamic script tags are a better solution than iframes...Google uses them for maps now (they used to use iframes, and they work poorly in comparison). Admittedly, you may have to do some work to make sure they don't conflict with the css etc on the site.
rob
That's actually why I recommended iFrames rather than dynamic script - it keeps the CSS completely separate and avoids any kind of conflicts.
EAMann
A: 

Assuming the AJAX stuff is right (which I haven't confirmed): You say you can't use PHP for this - if you just mean you need it to use javascript asynchronously but can still use server code in some places, what about using PHP (or any server-side language) to do the actual work and return it to the page through AJAX/javascript - this would solve the problem Alex brings up.

So instead of getting from mysite.com/something.txt from javascript, get it from SomeAjaxHelper.php (or aspx or whatever).

Joe Enos
It's a widget that other people can use on their websites, hence not being able to use server-side. The .txt file in question is automatically loaded with a new piece of "trivia" each day.
Cyprus106
How about server-code on your server that reads the trivia piece into a string wrapped in a javascript function, then pointing to that server page as script. Something like:<script src="http://mysite.com/trivia_getter.php" type="text/javascript"></script><script type="text/javascript"> span1.innerHTML = getTriviaText();</script>Your PHP file would use server code to read from the source file, then output something like:function getTriviaText() { return "{this part is filled in from server code}";}To the widget, your PHP file is just a simple javascript file.
Joe Enos
I hope that made sense...
Joe Enos
+1  A: 

This exmaple contains jQuery code.

var text; 
$.get( "proxy.php", function(data) {
   text = data.responseText; 
});

Then in proxy.php:

<?php
header('Content-type: application/xml');
$daurl = 'http://www.mysite.com/todaysTrivia.txt';
$handle = fopen($daurl, "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}

Example taken from here: http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html

As explained before, xmlhttp is designed for forbid cross domain requests for security issues. But nothing prevents you from doing this on your server in PHP.

Another example can be found here: http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide

elcuco
Warning: your warning is ridiculous. If you feel that you get flack for offering jquery solutions, try justifying them instead.
Justin Johnson
or you could read the entire posters OP, and realize he's trying to do something that can not be done, jQuery or not.
Erik
Well... yeah. that did it if I'm in the local host. Obviously, it does not work if I try to pull some jive like: $.get( "http://www.mysite.com/trivia-proxy.php", function(data) { That whole "not possible" thing is looking about right. sigh.
Cyprus106
that is exactly what I am showing you, the proxy is what lets you read the text from another domain, The proxy is on "localhost" (if the JS code is running in mysite.com, then proxy.php is also in mysite.com).
elcuco
A: 

For cross domain, you would have to use dynamic script tags to fetch data asynchronously. The todaysTrivia file would be a .js file that stores the data as JSON. Google for "dynamic script tags cross domain" if you want to use this technique.

rob