使用字符串回复以在 JSON 成功时发挥作用

Reply with string to function on JSON success

本文关键字:成功 作用 JSON 字符串 回复      更新时间:2023-09-26

我想在 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");
    }
});

把这一行放回去:

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

然后更改

alert(message);

alert (message.message)