JQUERY ---在Json中获取价值 - 无法正常工作

时间:2018-03-13 14:01:15

标签: jquery json

我在使用JSON字符串获取数组时遇到错误,我在下面简化了我的脚本以便于理解

这是我的返回MVC控制器

View controller-based status bar appearance

这是我的前端

   message = $"{ ViewData["fileLocation"]} , { id}  ";
            return Json(message);

1 个答案:

答案 0 :(得分:1)

不确定是什么,你试图在这里做。

message = $"{ ViewData["fileLocation"]} , { id}  ";

尝试返回(匿名)对象

var message = new { fileLocation = ViewData["fileLocation"], id = id};
return Json(message);

然后在JS

$.ajax({
    type: "POST",
    url: "/Upload/AjaxUpload",
    contentType: false,
    processData: false,
    data: formData,
    success: function (message) {
        alert(message);

        var msg = $.parseJSON(message);
        alert(msg.fileLocation);
        $(".classify").attr("src", message.fileLocation);

        $('#ajax_loading2')
            .html('Congrats image uploaded.\n Upload ID: ' + msg.id);