views:

63

answers:

2

Hi,

I currently have a controller method that returns a JsonResult:

    public JsonResult EditField(int FieldID, string FieldValue)
    {
            var f = ManagerProvider.GetFieldManager();
            try
            {
                f.UpdateField(FieldID, FieldValue);
                return Json(new { State = "Success", Message = "Success"});
            }
            catch (Exception ex)
            {
                return Json(new { State = "Error", Message = ex.Message });
            }
     }

When I post this using jQuery ($.post), the callback function is initiated, where I consume the returned Json object. I can print out the feedback, which appears as

{"State" : "Error", "Message" : "Invalid input"}

However, when I go to get individual parts of this in the Javascript, by using

alert(data.State);

All I get from this is "undefined".

Has anybody got any ideas please?

Cheers,

Chris

A: 

Have you tried using jQuery getJSON method:

http://docs.jquery.com/Ajax/jQuery.getJSON

Paddy
That will perform a GET operation instead of a POST.
Martijn Laarman
Fair enough. Should have read the question.
Paddy
+2  A: 

Are you positive that you specify "json" as return data type ?

$.postJSON = function(url, data, callback) {
    $.post(url, data, callback, "json");
};

Taken from the jQuery.post documentation page.

Martijn Laarman
Thanks for that! Worked a treat.
Chris