1

I'm using a code similar to the simplified one below to search through several pages of posts on a forum website and then list the links in console.

$(document).ready(function () {

for(i = 0; i <= 735; i += 15) {
    var xhrs = new XMLHttpRequest();
    xhrs.open("get", 'http://fakeURL.com/' + i, true);
    xhrs.onreadystatechange = function() 
    {
        if (this.readyState == 4) 
        {
            $(this.responseText).find('a').each(function()
            {           
                var url = $(this).attr('href');
                console.log(url);                                               
            });
        }
    }
    xhrs.send();
}

}

The problem is that sometimes I'll get errors like this in console:

Resource interpreted as Image but transferred with MIME type text/html: "dailydawdle.com/".

Error: GET http://puu.sh/4Ueom.png 404 (Not Found) 4Ueom.png:1

Error: GET http://thisUrlIsTooLong.jpg 403 (Forbidden) cellphonebuyers large verge super wide.jpg:1

These errors usually don't stop the code from finishing the list of urls, but one time it exited the webpage and went to a red screen in chrome that said something like 'security error'.

  1. Why do these errors occur and how do I stop them?

Even if I only search each page for links that begin with a certain string I get these errors. I'm saying that even if I replace find('a') with find("a[href^='http://forum.posts/']") so that only the forum's links are searched through, I still get errors with outsider website links (posted by people in the forum probably) and sometimes a red warning page that says 'security error'.

  1. Why am I getting errors on links that don't even begin with the string I specified to search for?
Swift
  • 54
  • 1
  • 8
  • I would first of all question the validity of a tight loop doing ajax. Instead execute the NEXT fetch in the success of the previous – mplungjan Oct 05 '14 at 07:41
  • See https://stackoverflow.com/a/12100954/938089, below the section "Why are these images loaded? I didn't put them in the document". – Rob W Oct 05 '14 at 09:26
  • Those are serverside errors, how would we know what causes them?! – Bergi Oct 05 '14 at 11:05
  • @RobW Disregard my last comment. It was false so I deleted it. The ACTUAL problem is that after implementing `xhrs.onload = function() {var doc2 = xhrs.response; //then a bunch of jQuery}` and `xhrs.responseType = 'document';` into my For-Loop code, it's only showing that the loop's final iteration occurs (which is when `i` is 0). Not sure why. – Swift Oct 05 '14 at 17:31
  • 1
    Here's a link to the new question: [link](http://stackoverflow.com/questions/26205592/xmlhttprequest-domparser-in-for-loop-returns-only-last-iteration) – Swift Oct 05 '14 at 18:22

0 Answers0