You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AG Grid SSRM原生JS客户端报错:TypeError: params.successCallback is not a function

AG Grid SSRM原生JS客户端报错:TypeError: params.successCallback is not a function

你遇到的这个问题其实是AG Grid版本更新带来的API变更导致的——你用的是v31.3.2版本的AG Grid Enterprise,而你参考的官方教程可能是基于旧版本编写的,新版本里SSRM数据源的getRows方法参数结构已经变了,原来的successCallback和failCallback这两个函数被移除了,换成了新的调用方式。

我给你两种修复方案,任选其一都能解决问题:

方案一:使用新版的回调方法

把你原来的datasource代码改成下面这样,用params.success()和params.fail()替代旧的回调函数:

const datasource = {
    getRows(params) {
        console.log(JSON.stringify(params.request, null, 1));

        fetch('http://localhost:4000/olympicWinners/', {
            method: 'post',
            body: JSON.stringify(params.request),
            headers: {"Content-Type": "application/json; charset=utf-8"}
        })
            .then(httpResponse => httpResponse.json())
            .then(response => {
                // 新版用params.success,传入包含rows和rowCount的对象
                params.success({ 
                    rows: response.rows, 
                    rowCount: response.lastRow // 注意:如果后端lastRow是总记录数直接用,如果是最后一行索引,可能要改成response.lastRow + 1
                });
            })
            .catch(error => {
                console.error(error);
                params.fail(); // 新版的fail方法
            })
    }
};

方案二:返回Promise(更简洁的写法)

AG Grid的新版SSRM支持让getRows直接返回Promise,这样你不用手动调用回调,代码更简洁:

const datasource = {
    getRows(params) {
        console.log(JSON.stringify(params.request, null, 1));

        // 直接返回fetch的Promise
        return fetch('http://localhost:4000/olympicWinners/', {
            method: 'post',
            body: JSON.stringify(params.request),
            headers: {"Content-Type": "application/json; charset=utf-8"}
        })
            .then(httpResponse => httpResponse.json())
            .then(response => {
                // 返回包含rows和rowCount的对象
                return {
                    rows: response.rows,
                    rowCount: response.lastRow
                };
            })
            .catch(error => {
                console.error(error);
                // 抛出错误会触发AG Grid的失败处理逻辑
                throw error;
            });
    }
};

注意事项

要确认后端返回的lastRow的含义:

  • 如果后端返回的是总记录数,直接赋值给rowCount即可;
  • 如果后端返回的是当前页最后一行的索引(比如旧教程里的逻辑),那你需要改成rowCount: response.lastRow + 1,这样AG Grid才能正确计算分页和滚动逻辑。

改完之后刷新页面,应该就能正常渲染数据了,因为数据本身已经能正常获取,只是回调方式不对导致Grid无法接收到数据。

备注:内容来源于stack exchange,提问作者Mohamed Ishad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 09:42:56