0

I am using javascript to pull data from a URL and put it into a div:

function ahah(url, target) {
    document.getElementById(target).innerHTML = 'Fetching fixtures...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
    }
}  

function ahahDone(url, target) {
    if (req.readyState == 4) { // only if req is "loaded"
        if (req.status == 200) { // only if "OK"
            document.getElementById(target).innerHTML = req.responseText;
        } else {
            document.getElementById(target).innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
        }
    }
}

function load(name, div) {
    ahah(name,div);
return false;
}

load('http://www.domain.com/feeds/','content');

The problem is that if the URL to load is local it works fine (/feeds/), but when I change it to point to an external site (http://www.domain.com/feeds/) where I intend to hold the file I get an 'Error 0'.

I have checked the URL has the content available but unless it is local it will not work. Any feedback is very welcome, thank you.

Paul
  • 2,465
  • 8
  • 35
  • 60
  • possible duplicate of [Can I load data from an external page via AJAX?](http://stackoverflow.com/questions/2409581/can-i-load-data-from-an-external-page-via-ajax) – Felix Kling Feb 02 '12 at 14:26
  • It is impossible to load data from another domain unless specific cases. – kirilloid Feb 02 '12 at 14:27

2 Answers2

1

An 'error' of 0 simply means that the request loaded on the local machine. Nothing to worry about :)

Just change this line of code to accept a status of 0:

if (req.status == 200 || req.status == 0) {

Edit: As some commenters have mentioned, you could have domain-origin problems as well, though I don't believe that is the problem in this instance.

Jeffrey Sweeney
  • 5,986
  • 5
  • 24
  • 32
  • Thank you for your response. I added this line and it got rid of the error, but no content has loaded into the div. (It still works if I have it local). – Paul Feb 02 '12 at 14:37
  • No errors in the console even? Try sticking the whole function in a `try` `catch` block and see if anything happens. Also, what browser are you using? – Jeffrey Sweeney Feb 02 '12 at 14:39
  • Hi Jeffrey. No errors in the console and I tried the 'try and catch', no errors returned. It just doesn't like using a remote server :( – Paul Feb 02 '12 at 14:45
  • And that domain definitely exists? I don't know what else to advise, sorry :| – Jeffrey Sweeney Feb 02 '12 at 14:48
  • Yes the URL with the content defiantly exists. I have opened the javascript file via the page I am trying to load it on, copied and pasted the url into the address bar and its all there. – Paul Feb 02 '12 at 14:54
1

I found out that the html pages you are trying to fetch should reside on your website (domain). The html fragments you try to fetch from other sites wont be fetched.

amplifire
  • 11
  • 1