简体   繁体   中英

JQuery - Fetching a URL that returns 404 on first few fetches, then returns 200

Fetch a URL - mywebiste.com/blah.php --> returns 404

Fetch a URL - mywebsite.com/blah.php --> returns 404

Fetch a URL - mywebsite.com/blah.php --> returns 404

Fetch a URL - mywebsite.com/blah.php --> returns 200 // how do I loop until this the URL returns 200?

Previously phrased:

So I have a URL (foo) that I need to download after my page has finished rendering or the element that I want to show it in is on the page ie I'll call this function that I'm asking about from my element.

The problem is I have to keep downloading the URL until it returns 200, it starts of saying 404, and gives 200 and a bunch of other headers, once it's done.

Can anyone think of a quick and easy way of doing this? I'm sort of brain-dead @ 2:50AM :(

Try something like this - load the page, and try again and again on error. Make sure to add the rest of the arguments to ajax . You can add a check to load only on error code 404:

function tryLoad
    $.ajax({
        url: 'url',
        success: function(data){ /* handle load */ },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(tryLoad, 1000); //try again later
        }
    });
}

Of course, this will load indefinitely if you have a real error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM