我进行了jquery ajax调用,调用了action方法并返回数据,但结果是内部服务器错误。
你能否解释一下这个问题?
以下是代码:
$.ajax({
url: "/Post/GetFieldInformation",
data: { feedID: feedID, asUserID: $('#AsUserID').val(), fieldHandled: @Html.Raw(JsonConvert.SerializeObject(Model.FieldHandled)) },
type: 'GET',
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (mydata) {
alert("success");
},
error: function (request, status, error) {
alert(error);
}
});
行动方法:
[HttpGet]
public JsonResult GetFieldInformation(string feedID, int asUserID, Dictionary<int, bool> fieldHandled)
{
FieldInformation result = new FieldInformation();
string[] feedIDs = new string[] { feedID };
result.Fields = dr.UserFields(this.RequestCultureID, asUserID, feedIDs, new string[0], !base.CurrentUserSessionInfo.FeatureAllOptionalFields);
result.SpecificFields = result.Fields.Where(p => p.Key > 1000 && p.Value.FeedUsing == 1 && !fieldHandled.ContainsKey(p.Key))
.OrderBy(p => p.Value.SortOrder).ThenBy(p => p.Value.FieldTypeID).ThenBy(p => p.Value.FieldLabel);
return Json(result, JsonRequestBehavior.AllowGet);
}
以下是回复文字: ZmSHUzueqrxob.62fF。Q \ 9Z 9I =ȇ9C
答案 0 :(得分:1)
我的猜测是,如果字典为空,则不进行序列化。你可能想尝试这样的事情
public JsonResult GetFieldInformation(string feedID, int asUserID, Dictionary<int, bool> fieldHandled = null)
{
fieldHandled = fieldHandled ?? new Dictionary <int, bool>()
}
修改强>
在进行序列化时,请尝试在JSON.Net库中使用Newtonsoft.Json.Converters.KeyValuePairConverter对象:
var json = JsonConvert.SerializeObject( package, new KeyValuePairConverter( ));
return Json(json , JsonRequestBehavior.AllowGet);