简体   繁体   English

jQuery-获取一个URL,该URL在前几次获取时返回404,然后返回200

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

Fetch a URL - mywebiste.com/blah.php --> returns 404 获取URL-mywebiste.com/blah.php->返回404

Fetch a URL - mywebsite.com/blah.php --> returns 404 获取URL-mywebsite.com/blah.php->返回404

Fetch a URL - mywebsite.com/blah.php --> returns 404 获取URL-mywebsite.com/blah.php->返回404

Fetch a URL - mywebsite.com/blah.php --> returns 200 // how do I loop until this the URL returns 200? 获取URL-mywebsite.com/blah.php->返回200 //如何循环播放,直到URL返回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. 因此,我有一个URL(foo),需要在页面完成渲染或要显示的元素在页面上后下载,例如,我将从我的元素中调用该函数。

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. 问题是我必须继续下载URL,直到返回200,然后开始说404,并在完成后给出200和一堆其他标头。

Can anyone think of a quick and easy way of doing this? 谁能想到快速简便的方法? I'm sort of brain-dead @ 2:50AM :( 我有点脑筋急转@ 2:50 AM :(

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 . 确保将其余参数添加到ajax You can add a check to load only on error code 404: 您可以添加检查以仅在错误代码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. 当然,如果您遇到真正的错误,它将无限期加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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