开发者

jQuery and ASP.NET MVC - JSON Objects

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


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.


Have you tried using jQuery getJSON method:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜