0

我正在使用 jQuery 执行 AJAX 请求,它工作正常,但是服务器响应在带有 URL 的 XML 响应和没有内容的 XML 响应之间变化。

实际上,这两个响应都将请求限定为“成功”或“完成”,因此在处理我的 AJAX 请求的“完成”事件时,我想验证响应不为空,如果是,请尝试延迟后再次 AJAX 请求。

我正在研究 jQuery 承诺,但我不确定这是否正是我想要的,但如果是,请提供一个易于理解的示例。

这就是我想要的:

    function(name, location){
        $.ajax({
            type: "POST",
            url: "some.php",
            data: { name: name, location: location }
        }).done(function( msg ) {
            if (msg.length < 1) {
                setTimeout(
                   // Perform this request again
                , 500);
            } else {
                // Go on
            }
        });
    }
4

1 回答 1

0

只是一个猜测

//here´s a function called runAgain, waiting for two parameters, the timeout function will execute it again
function runAgain(name, location){
            $.ajax({
                type: "POST",
                url: "some.php",
                data: { name: name, location: location }
            }).done(function( msg ) {
                if (msg.length < 1) {
                    setTimeout(function(){
                       runAgain(name,location)
                    }, 500);
                } else {
                    // Go on
                }
            });
}

//This function runs when the document is ready, an calls the runAgain function 
$(function(){

   var name= "?";
   var location = "?";
    runAgain(name,location);
});
于 2013-10-09T22:26:01.377 回答