-2

I make a jquery ajax call, the action method is called and returns data, but the result is Internal Server Error.

Could you please shed light on this problem?

Here is the code:

$.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);
    }
});

Action Method:

[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);
}

Here is the response text: �ZmS�H��U�z�u���e��qrxo��b.�����6�2��fF����…�q��\�9���Z����9�i=ȇ���9C�

Here is what is returned by the ActionMethod: Here is what is returned by the ActionMethod:

1 Answers1

1

My guess is that its not serializing if the dictionary is null. You Might wanna try something like this

public JsonResult GetFieldInformation(string feedID, int asUserID, Dictionary<int, bool> fieldHandled = null)
{
    fieldHandled = fieldHandled ?? new Dictionary <int, bool>()
}

Edit

Try using Newtonsoft.Json.Converters.KeyValuePairConverter object in the JSON.Net library when doing your serialization:

  var json = JsonConvert.SerializeObject( package, new KeyValuePairConverter( ));
  return Json(json , JsonRequestBehavior.AllowGet);
johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • I know it is not null, and even if it was, the resulting object has data. – Dmitriy Reznik Aug 12 '15 at 20:24
  • 1
    It must be the resulting data. If I return just Json("something"), it succeeds. But I don't know what is wrong with the result. Please see what is inside in my updated question. – Dmitriy Reznik Aug 13 '15 at 16:55
  • It's probably because your trying to serialize a dictionary, the built in json doesn't do that too well I'll post an answer in a second – johnny 5 Aug 13 '15 at 18:25
  • let me know if you have any other issue i'd be happy to help – johnny 5 Aug 13 '15 at 19:42