简体   繁体   中英

How to get paginated results from jQuery.ajax

My apology if this question has been asked a dozen times already. My incapable search couldn't land me the answer right away. I would like to get paginated results from Ajax, and run some function on the returned result. The function is the following.

function getPagedAjax(url, data,type='GET'){
        jQuery.ajax({
                type: type,
                url : url,
                data: data,
                dataType: 'JSON',
                success: function(response) {
                }
        }).then(
                function(response1){
                        console.log(response1);
                        const pages = Math.ceil ( response1.data.total / itemsPerPage);
                        const dataArray = [...Array(pages + 1).keys()].slice(2).map(
                                function(page){
                                        var pagedData = jQuery.extend({}, data);
                                        pagedData.page=page;
                                        return pagedData
                                }
                        ); //build an array of json data for API calls
                        return jQuery.when( ...dataArray.map( data2 => jQuery.ajax(
                                {type: type, url: url, data: data2, dataType: 'JSON'}))).then(
                                        function(){
                                                console.log( [response1].concat(Array.from(arguments)));
                                                return [response1].concat(Array.from(arguments))
                                        }
                                )
                }
        )
}

and I expect the result to be caught as follows.

getPagedAjax(url, data).done(function(msg){console.log(msg);});

However, I get 'Undefined' instead. I would appreciate some guidance where my logic went wrong.

I missed return

function getPagedAjax(url, data,type='GET'){
        return jQuery.ajax({
                type: type,
                url : url,
                data: data,
                dataType: 'JSON',
                success: function(response) {
                }
        }).then(
                function(response1){
                        console.log(response1);
                        const pages = Math.ceil ( response1.data.total / itemsPerPage);
                        const dataArray = [...Array(pages + 1).keys()].slice(2).map(
                                function(page){
                                        var pagedData = jQuery.extend({}, data);
                                        pagedData.page=page;
                                        return pagedData
                                }
                        ); //build an array of json data for API calls
                        return jQuery.when( ...dataArray.map( data2 => jQuery.ajax(
                                {type: type, url: url, data: data2, dataType: 'JSON'}))).then(
                                        function(){
                                                console.log( [response1].concat(Array.from(arguments)));
                                                return [response1].concat(Array.from(arguments))
                                        }
                                )
                }
        )
}

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