Ajax调用导致内部服务器错误

时间:2015-08-12 20:07:40

标签: jquery ajax asp.net-mvc

我进行了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

以下是ActionMethod返回的内容: Here is what is returned by the ActionMethod:

1 个答案:

答案 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);