如何访问JSON对象中的值?

时间:2014-05-27 08:53:43

标签: javascript ajax json response

status是否是可用的JSON对象?

如何在action_success上访问newIndexstatus的值?

服务器:

[HttpPost]
public ActionResult UploadFiles()
{
    // save file..
    return Json(new { action_success = "true", newIndex = 2 });
}

客户端:

complete: function (e, data) {
    var status = e.currentTarget.response;
    // From FireBug: status is "{"action_success":"true","newIndex":2}"
    // Including the first and last double-quote.

    // I want to do something like:
    // status.action_success and status.newIndex but I can't!    
}

修改 简单的解决方案:

var statusParsed = JSON.parse(status);
var success = statusParsed.action_success;
var index = statusParsed.newIndex;

1 个答案:

答案 0 :(得分:0)

我认为您可以直接使用JsonResult并且应该可以使用

[HttpPost]
public JsonResult UploadFiles()
{
// save file..
return Json(new { action_success = "true", newIndex = 2 });
}

在客户端如果使用$ .ajax(),则将数据类型设置为json