Datatables.net uncaught typeerror:无法读取属性'length' [英] Datatables.net uncaught typeerror: cannot read property 'length'

查看:92
本文介绍了Datatables.net uncaught typeerror:无法读取属性'length'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am using datatables.net plug in to load data in table.
I called web method in asp.net page and return 
JsonConvert.SerializeObject(result) 
as string; 
I could see below json string as response but I do not know how to bind them with datatables.net

{"d":"[{\"Apple\":507,\"Orange\":6,\"Mango\":75,\"Peach\":638,\"City\":\"SAN ANTONIO\",\"State\":\"TX\"},{\"Apple\":507,\"Orange\":6,\"Mango\":75,\"Peach\":638,\"City\":\"SAN ANTONIO\",\"State\":\"TX\"}]"}

< br $> b $ b

我尝试了什么:



我试过



What I have tried:

I tried to

JsonConvert.SerializeObject(new {data =result})

推荐答案

DataTables期望返回的值是一个带有属性的对象 data ,其中包含要显示的数据列表:

ajax |选项| DataTables.net [ ^ ]



ASP.NET WebMethod总是将返回的对象包装在一个名为 d 的属性的对象中,以避免潜在的CSRF如果结果是数组则发出问题:

一个微妙的JSON漏洞剖析|你被劫持了 [ ^ ]



您还对结果进行了双重编码 - 您的Web方法应该只返回数据直接,而不是JSON编码。



一旦你改变你的web方法去除额外的JSON编码,你可以使用脚本中的dataFilter 选项,用于修改返回的数据以匹配预期的格式。例如:

DataTables expects the returned value to be an object with a property called data which contains the list of data to display:
ajax | Options | DataTables.net[^]

An ASP.NET WebMethod always wraps the returned object in an object with a property called d, to try to avoid a potential CSRF issue if the result is an array:
Anatomy of a Subtle JSON Vulnerability | You’ve Been Haacked[^]

You've also double-encoded the result - your web method should just return the data directly, rather than JSON-encoding it.

Once you've changed your web method to remove the extra JSON encoding, you can use the dataFilter option in your script to modify the returned data to match the expected format. For example:
"ajax": {
    ...
    dataFilter: function (res) {
        var parsed = JSON.parse(res);
        return { data: parsed.d };
    }
},


这篇关于Datatables.net uncaught typeerror:无法读取属性'length'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆