1

I need to get json object from string and return it from controller.

In my Controller I have second thing:

    [HttpGet]
    public JsonResult GetPageFilters()
    {
        ...
        ...

        if (settings != null)
        {
            var data = JsonConvert.DeserializeObject(settings.Filters); //Filter is string with json

            return Json(data);
        }
        else
        {
            return null;
        }            
    }

and this in my view:

    var filterOption;

    $.get('Library/Books/GetPageFilters', null, function(data) {
        filterOption = data;
    }, "json");

Controller are called normally, string is deserialized to object... but function(data) is not working. Nothing are happening there and I cant get why.

What am I missing?

EDIT:

Looks like string to json went wrong. I tried to save it in database what looks fine i guess, but read it properly is a problem to me.

function UpdateFilter() {

    var filterOption = {
        "filterTarget": "Books",
        "filters": [
            { "cancelled": $("#showCancelledFilter").is(':checked') },
            { "completed": $("#showAllFilter").is(':checked') }
        ],
        "page": page,
        "sorting": sorting
    };

    var url = "Library/Books/UpdateFilter";
    $.post(url, { pageFilters: JSON.stringify(filterOption) }, function (data) { });
}

seems working fine, but as I told already, from string to json is not ok for some reason.

Olegs Jasjko
  • 2,128
  • 6
  • 27
  • 47
  • What do you mean by 'nothing is happening'? Do you get inside the success handler? – Vsevolod Goloviznin Aug 18 '15 at 08:06
  • 9
    Because its a GET method. You need `return Json(data, JsonRequestBehavior.AllowGet);` –  Aug 18 '15 at 08:06
  • Does that mean that I actually don't need a `[HttpGet]` ? – Olegs Jasjko Aug 18 '15 at 08:10
  • You could make it `[HttpPost]`, but you dont seem to be changing any data, so a GET seems more appropriate –  Aug 18 '15 at 08:12
  • Thanks, could you please make your comment as an answer so I can mark it? Aslo... I got data in view... but looks loke deserializing went wrong (I got a 4 Arrays (array on each field in json and empty array inside each array)... I will describe in edit, how i tried to dave json in database and then get it from string. – Olegs Jasjko Aug 18 '15 at 08:18

2 Answers2

2

Your method is marked with the [HttpGet] which means you need to change you method to

return Json(data, JsonRequestBehavior.AllowGet);

By default, the JsonRequestBehavior is set to DenyGet

You can read more about this in the answers to this question

Community
  • 1
  • 1
  • Thanks. If you have some time, could you please view the edit where is described change json to string and its writing to database, because that I received after deserialization is not what I needed. :( – Olegs Jasjko Aug 18 '15 at 08:28
  • Eh, well, will make it as another question :) – Olegs Jasjko Aug 18 '15 at 08:37
1

You are missing JsonRequestBehavior.AllowGet

[HttpGet]
    public JsonResult GetPageFilters()
    {
        ...
        ...

        if (settings != null)
        {
            var data = JsonConvert.DeserializeObject(settings.Filters); //Filter is string with json

            return Json(data, JsonRequestBehavior.AllowGet);  
        }
        else
        {
            return Json(null, JsonRequestBehavior.AllowGet);  
        }            
    }
Muks
  • 134
  • 9