首页 > 解决方案 > 无法使用ajax将数据加载到表中

问题描述

我真的很难将现有代码从 3.5 升级到 4.0.5。这一次,当我尝试使用 ajax 调用将数据加载到我的表中时,我遇到了一个错误。这在 3.5 中确实有效,所以我猜版本 4 中发生了一些变化。我仔细阅读了文档并阅读了升级指南。ajax 调用的方式是否发生了变化,以至于我的代码不能像在 3.5 中那样工作?最后:我正在使用 jquery 的包装器。

表构造函数如下所示:

$("#PO-table").tabulator({
...columns etc
ajaxResponse: function (url, params, response) {


                        //url - the URL of the request
                        //params - the parameters passed with the request
                        //response - the JSON object returned in the body of the response.

                        return response.d; //Return the d Property Of a response json Object
                    },
});

然后我通过setData添加了很多参数,以及之前声明的ajaxconfig

var ajaxConfig = {
                type: "POST", //set request type to Position
                contentType: 'application/json; charset=utf-8', //set specific content type
            };

$("#PO-table").tabulator("setData", "PurchaseOrder.aspx/Fetch_PurchaseOrders", "{'POnum': '" + ponum + "', 'supplier': '" + supp + "', 'fromDate': '" + from + "', 'toDate': '" + to + "', 'spareNumber': '" + spare + "', 'isDelivered': '" + isdelivered + "', 'isConfirmedOrder': '" + true + "', 'isUnconfirmedOrder': '" + true + "', 'isExactPOnum': '" + false + "', 'isExactSupp': '" + false + "'}", ajaxConfig); 

然后我在控制台中收到错误,数据不会加载到表中: 在此处输入图像描述

标签: tabulator

解决方案


默认情况下,Tabulator 会将 POST 请求中的数据作为表单数据发送,如果您想将其作为 JSON 发送,则需要使用ajaxContentType选项

var table = new Tabulator("#example-table", {
    ajaxURL:"http://www.getmydata.com/now", //ajax URL
    ajaxConfig:"POST", //ajax HTTP request type
    ajaxContentType:"json", // send parameters to the server as a JSON encoded string
});

这会将数据编码为 JSON 对象并设置适当的标头。

ajaxContentType选项是在 4.1版本中添加的,以使用户更容易将具有各种内容类型的请求发送回他们的服务器


推荐阅读