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.

5
  • What do you mean by 'nothing is happening'? Do you get inside the success handler? Commented Aug 18, 2015 at 8:06
  • 9
    Because its a GET method. You need return Json(data, JsonRequestBehavior.AllowGet); Commented Aug 18, 2015 at 8:06
  • Does that mean that I actually don't need a [HttpGet] ? Commented Aug 18, 2015 at 8:10
  • You could make it [HttpPost], but you dont seem to be changing any data, so a GET seems more appropriate Commented Aug 18, 2015 at 8: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. Commented Aug 18, 2015 at 8:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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. :(
Eh, well, will make it as another question :)
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);  
        }            
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.