【问题标题】:How to get paginated results from jQuery.ajax如何从 jQuery.ajax 获取分页结果
【发布时间】:2020-12-23 21:16:56
【问题描述】:

如果这个问题已经被问了十几次,我深表歉意。我无能的搜索无法立即为我找到答案。 我想从 Ajax 获取分页结果,并在返回的结果上运行一些函数。 函数如下。

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))
                                        }
                                )
                }
        )
}

我希望结果如下。

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

但是,我得到的是“未定义”。如果我的逻辑出错了,我将不胜感激。

【问题讨论】:

    标签: javascript jquery ajax promise


    【解决方案1】:

    我错过了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))
                                            }
                                    )
                    }
            )
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 2013-08-24
      • 2016-08-03
      • 2012-04-07
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多