tabulator - 无法使用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 会将 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版本中添加的,以使用户更容易将具有各种内容类型的请求发送回他们的服务器
推荐阅读
- html - BOX并排放置
- python-3.x - 如何在发布请求键中传递变量?
- laravel - 自动完成找不到路线
- c - 当字符串被 C 中的空字符截断时会发生什么?
- javascript - 如何从 Javascript 中另一个元素中包含的 div 获取信息?
- python - 使用烧瓶上传文件的安全内容
- linux - 使用 Shell 从 Windows 机器发送邮件(来自 Git bash)
- c++ - 如何指定自定义类符合标准或自定义类型特征
- c++ - 类的取消引用不起作用,并且此错误不断弹出:“std::logic_error what(): M constructor null not valid”
- c++ - ESP32 任务中的 RTC DS3231 在读取温度时导致 ESP 重启