Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重复HTTP请求,直到返回404错误_Javascript_Angularjs_Http - Fatal编程技术网

Javascript 重复HTTP请求,直到返回404错误

Javascript 重复HTTP请求,直到返回404错误,javascript,angularjs,http,Javascript,Angularjs,Http,我想发送http请求,直到其中一个响应为错误404 共有21页,我有如下内容: _getAll = function () { var promises = []; var pages = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]; angular.forEach(pages, function (page) { var deffered = $q.defer(); $http({

我想发送http请求,直到其中一个响应为
错误404

共有21页,我有如下内容:

_getAll = function () {

var promises = [];
var pages = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];
angular.forEach(pages, function (page) {
    var deffered = $q.defer();
        $http({
            url: 'http://api.tvmaze.com/shows?page=' + page,
            method: 'GET'
        }).
            success(function (data) {
                console.log("OK")
                deffered.resolve(data);
            }).
            error(function (error) {
                deffered.reject(error);
                console.log(error.status);
            });
        promises.push(deffered.promise)
    })
    return $q.all(promises);
},
但如果我尝试获取,它将返回
404错误


那么,是否有一个选项可以发出
http
请求,直到其中一个返回
404
。在循环或其他情况下?

这些调用是异步的,因此在加载第一页之前,while循环将尝试在几毫秒内调用1000页。在加载下一页之前,您需要等待每一页完成

一种方法是创建一个函数来获取一个页面,并在每次成功加载页面时用下一个页码触发它。一旦遇到错误,触发最后一次成功回调并将数据传回

_getAll = function(callback) {

    var pageData=[];

    function getPage(page) {
        $http({
            url: 'http://api.tvmaze.com/shows?page=' + page,
            method: 'GET'
        }).
        success(function (data) {
            pageData.push(data);
            //get next page
            getPage(page + 1);
            console.log("OK")
        }).
        error(function (error) {
            //Hit an error. All done. Trigger callback.
            callback(pageData);
            console.log(error.status);
        });
    }

    //get first page
    getPage(0);
}

//usage:

_getAll(function(data){
    //this function will trigger once we get an error
    //data will be an array of all of the pages data
   console.log(data); 
});

$q.all
通过正确的错误处理将允许您执行此操作。以下是代码及其解释:

_getAll = function() {

    var pages = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
    return $q.all(pages.map(function(page) {
        return $http({
            url: 'http://api.tvmaze.com/shows?page=' + page,
            method: 'GET'
        })
        .then(function(response) {
            return response.data;
        }, angular.noop);
    }))
    .then(function(data) {
        data.pop();
        return data;
    });
};
注意,
angular.noop
中,然后
回调各个请求。它基本上是在没有更多页面时处理失败请求的方法(状态404)。当您进入这个错误回调(它什么也不做)时,由于它什么也不做,这意味着您可以从错误情况中恢复,并且所有以前成功的请求数据将进一步传播。在下一个
中,我们需要删除最后一个
未定义的
元素(404页
angular.noop
返回的元素),这样生成的数据数组就不会像最后一个元素那样有无意义的未定义值

演示:

取消承诺?-这将返回一个空数组,而不是404。