回复字符串以在JSON成功时起作用

时间:2014-11-26 21:04:28

标签: javascript asp.net-mvc json

我想在JSON返回时向JS函数发回一条消息。 正如您在下面看到的,我试图将消息插入到JSON回复中,但我得到[object][object] 而不是消息。我该如何阅读该消息?

这是我的代码:

控制器:

 public ActionResult UpdateCategory(Category category)
    {

         // do something.



     // I want to send this message back to the function:
        var message = "some message to reply."  


      // I was trying this:
      // return Json(new { success = true, message = message }, JsonRequestBehavior.AllowGet);


     return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }

脚本

$.ajax({
    type: "POST",
    url: "/UpdateCategory",
    data: formData,
    dataType: 'json',
    contentType: false,
    processData: false,
    success: function (message) {  

       alert(message); // I would like to show here the message.                

    },
    error: function () {
        $('.overlay').hide();
        alert("error");

    }

});

1 个答案:

答案 0 :(得分:2)

放回这一行:

return Json(new { success = true, message = message }, JsonRequestBehavior.AllowGet);

然后改变

alert(message);

alert (message.message)