我有一个asp.net MVC 3控制器操作方法如下:
public JsonResult GetRecordingRates(int Id) { List<DefaultRateChart> defaultRateCharts = new List<DefaultRateChart>(); using (IDefaultRateChartManager defaultRateChartManager = new ManagerFactory().GetDefaultRateChartManager()) { defaultRateCharts = defaultRateChartManager.GetAll().Where(rc => rc.Currency.Id == Id && (!rc.NumberPrefix.StartsWith("#") || rc.NumberPrefix.StartsWith("Default")) && rc.AccountCredit == "Credit").ToList(); } return Json(defaultRateCharts); }
我想发送这个列表到jquery ajax成功的方法,但我得到500内部服务器错误
我的ajax电话是这样的:
$.ajax({ type: "POST",dataType: "json",url: "/Home/GetRecordingRates",data: { Id: $("#hdCurrencyId").val() },success: function (data) { alert(data); } });
在firebug XHR的响应标签下,它说:
在序列化“System.Reflection.RuntimeModule”类型的对象时检测到循环引用.
[编辑]
我把这个动作方法改成了:
public JsonResult GetRecordingRates(int Id) { List<DefaultRateChart> defaultRateCharts = new List<DefaultRateChart>(); using (IDefaultRateChartManager defaultRateChartManager = new ManagerFactory().GetDefaultRateChartManager()) { defaultRateCharts = defaultRateChartManager.GetAll().Where(rc => rc.Currency.Id == Id && (!rc.NumberPrefix.StartsWith("#") || rc.NumberPrefix.StartsWith("Default")) && rc.AccountCredit == "Credit").ToList(); } return this.Json( new { Result = (from obj in defaultRateCharts select new { Id = obj.Id,DialPrefix = obj.NumberPrefix,Rate = obj.PurchaseRates }) },JsonRequestBehavior.AllowGet ); }
我现在没有得到这个错误,但是如何解析ajax成功的集合.我改变了成功跟随,但它没有在表中添加行.
success: function (data) { var row = $('<tr>'); for(var i = 0; i < data.length; i++) { row.append($('<td>').html(data[i])); } $('#results').append(row); jQuery('#RecordingRates').dialog({ cloSEOnEscape: false }); $(".ui-dialog-titlebar").hide(); $("#RecordingRates").dialog({ dialogClass: 'transparent' }); $('#RecordingRates').dialog('open'); } });
在Firebug Net => XHR => Json,它显示以下JSON:
[Object { Id= 1,DialPrefix= "1",Rate= 2.6 },Object { Id= 3,DialPrefix= "2",Object { Id= 5,DialPrefix= "7",Rate= 3.5 },3 more...] 0 Object { Id= 1,Rate= 2.6 } 1 Object { Id= 3,Rate= 2.6 } 2 Object { Id= 5,Rate= 3.5 } 3 Object { Id= 7,DialPrefix= "8",Rate= 6 } 4 Object { Id= 9,DialPrefix= "Default",Rate= 5 } 5 Object { Id= 15,DialPrefix= "Subscription",Rate= 15 }
解决方法
how can I parse the collection in ajax success. I changed success to
following but it is not adding rows in table.
您正在返回一个包含以下字段的集合:Id,DialPrefix,Rate,而您没有使用它们.
var row = $('<tr>'); for(var i = 0; i < data.length; i++) { row.append($('<td>').html(data[i])); } $('#results').append(row);
您需要将它们添加到您的循环中:
row.append($('<td>').html(data[i].DialPrefix)); row.append($('<td>').html(data[i].Rate));