我在ASP.NET中使用MVC运行ASP.NET,我有一个ajax调用,它应该返回对象的list
,但它只是返回字符串"System.Collections.Generic.List`1[Namespace.CustomObject]"
显然,数据正在返回到javascript,而返回List<int>
并没有改变任何重要的东西,因此对象没有错误。我是在我的ajax调用中犯了一个丢失的错误,还是需要使用list
以外的其他东西?
我的ajax调用:
$.ajax({
type: 'POST',
url: url,
data: arrayOfInts
contentType: 'application/json',
datatype: 'json',
success: function (data) {
if (data.length) {
doThisIfDataReturned(data);
} else {
doThisIfNoDataReturned(productIds);
}
}
});
它调用的方法是:
public List<CustomObject> MakeAList(int[] productIds)
{
//
// create objectList
//
return objectList; //debugger shows that list is correct here
}
发布于 2015-07-24 16:24:01
在C#中,您需要返回一个JSON对象而不是一个列表。我以前也做过这样的事:
public JsonResult myFunc()
{
..... code here .....
return Json(myList);
}
编辑:有时候,在发送之前看到确切的返回内容是很好的。实现这一目标的一种方法是将返回对象赋值给变量,然后返回变量。
public JsonResult myFunc()
{
..... code here .....
var x = Json(myList);
return x;
}
这做了完全相同的事情,但稍微容易调试。
https://stackoverflow.com/questions/31615209
复制相似问题