我有一个C#Web项目,该项目具有一些REST请求,可以从数据库获取数据。我的问题是:我有一个GET调用,它返回查询返回的行数大小的数组,但每个对象都是空的。在调试时,每个对象都有我需要的所有数据,但是当数据到达javascript时,数组中就充满了空对象。我在哪里错了?
这是对.js文件的请求:
$.get("api/giacenzemobile/getGiacenze", function (data) {
console.log(data)
});
这是模型文件(.cs)上的功能
public static List<GiacenzeMobile> EstraiGiacenzeMobile()
{
List<GiacenzeMobile> giacenzeMobile = new List<GiacenzeMobile>();
SqlCommand cmd = null;
SqlConnection sqlconn = null;
using (sqlconn = new SqlConnection(Configurazione.ConnessioneDB))
{
sqlconn.Open();
string query = "...";
cmd = new SqlCommand(query, sqlconn);
SqlDataReader res = cmd.ExecuteReader();
while (res.Read())
{
GiacenzeMobile giac = new GiacenzeMobile();
giac.IdGiacenza = res.GetInt32(0);
//..... set all data ...
giacenzeMobile.Add(giac);
}
return giacenzeMobile;
}
}
这是控制器(.cs):
public class GiacenzeMobileController : ApiController
{
[Route("api/giacenzemobile/getGiacenze")]
public IEnumerable<GiacenzeMobile> GetGiacenze()
{
return GiacenzeMobile.EstraiGiacenzeMobile();
}
//...other code...
}
这是将结果写入控制台日志:
Array(6)0:{} 1:{} 2:{} 3:{} 4:{} 5:{}
已解决
我没有将GiacenzeMobile
对象的字段设置为公共。