jQuery和ASP.NET MVC - JSON对象

时间:2022-03-22 17:54:03

I currently have a controller method that returns a JsonResult:

我目前有一个返回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

当我使用jQuery($ .post)发布时,会启动回调函数,我将使用返回的Json对象。我可以打印出反馈,显示为

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

{“状态”:“错误”,“消息”:“输入无效”}

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

但是,当我在Javascript中通过使用获取此单独部分时

alert(data.State);

All I get from this is "undefined".

我得到的只是“未定义”。

Has anybody got any ideas please?

有人有任何想法吗?

Cheers,

Chris

2 个解决方案

#1


2  

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

您是否肯定将“json”指定为返回数据类型?

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

Taken from the jQuery.post documentation page.

取自jQuery.post文档页面。

#2


-1  

Have you tried using jQuery getJSON method:

您是否尝试过使用jQuery getJSON方法:

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

#1


2  

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

您是否肯定将“json”指定为返回数据类型?

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

Taken from the jQuery.post documentation page.

取自jQuery.post文档页面。

#2


-1  

Have you tried using jQuery getJSON method:

您是否尝试过使用jQuery getJSON方法:

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