gpt4 book ai didi

javascript - 从 "data"返回 JSON 数据

转载 作者:行者123 更新时间:2023-11-27 22:41:29 24 4
gpt4 key购买 nike

我需要用我的数据库数据填充 jQuery 数据表,但是很难做到这一点。在网上搜索了一下,我相信我已经找到了原因,这可能是我的 Controller 返回的json格式。格式应如下:

{
"data": [
{
"ID": "1",
"CODIGO": "CC0050",
"TEXTO": "USINAGEM"
},
{
"ID": "2",
"CODIGO": "CC0100",
"TEXTO": "MONTAGEM"
}]
}

但以这种格式返回:

[
{
"ID":24,
"CODIGO":"CC0050",
"TEXTO":"USINAGEM"
},
{
"ID":25,
"CODIGO":"CC0100",
"TEXTO":"MONTAGEM"
}
]

这是 Controller 代码:

[HttpGet]
public JsonResult GetAllCECOS()
{
using (RPIEntities contextObj = new RPIEntities())
{
try
{
var listaCECOS = contextObj.T0CECOS.ToList();
return Json(listaCECOS, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{

throw ex;
}
}
}

最佳答案

谢谢大家,

我更改了 JsonResult 方法的返回:

[HttpGet]
public JsonResult GetAllCECOS()
{
using (RPIEntities contextObj = new RPIEntities())
{
try
{
var listaCECOS = contextObj.T0CECOS.ToList();
**return Json(listaCECOS, JsonRequestBehavior.AllowGet);**
}
catch (Exception ex)
{

throw ex;
}
}
}

对此:

return Json(new { data = listaCECOS }, JsonRequestBehavior.AllowGet);

关于javascript - 从 "data"返回 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38721096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com