JsonResult被浏览器解释为字符串

时间:2014-11-10 09:01:37

标签: c# asp.net asp.net-mvc

我有一个像这样的控制器动作(非常简化):

[HttpPost]
public JsonResult Submit()
{
    Response.StatusCode = Convert.ToInt32(HttpStatusCode.BadRequest);
    return new JsonResult
    {
        ContentEncoding = Encoding.Default,
        ContentType = "application/json",
        Data = new {error = "xxxxxxxxxx"}
    };
}

关键是我想要返回json,但浏览器中的结果是字符串。 这是返回对象的属性: enter image description here

现在要使用它,我必须做JSON.Parse之类的事情,我真的不想这样做。控制器动作应该只返回json。

我之前在JavaScript中的ajax请求中看到了结果对象的responseJSON属性。

编辑: 我正在使用jQuery表单插件,所以从技术上讲,是谁发出请求。 这是我初始化jQuery Form Plugin的代码:

function initializeAjaxForm() {
    var feedback = document.getElementById('feedback');
    $('#upload-form').ajaxForm({
        url: '/xxxx/Submit',
        type: 'POST',
        beforeSubmit: handleBeforeSubmit,
        beforeSerialize: handlePreSerialize,
        success: function (data) {
            stopLoadingGif();
            feedback.innerHTML = 'Yay!';
            console.log(data);
        },
        error: function (data) {
            debugger;
            console.log(data);
            stopLoadingGif();
            feedback.innerHTML = 'Nay!';
        }
    });
}

以下是浏览器中的请求:

enter image description here

编辑2: 这是响应标题:

enter image description here

EDIT3:

这似乎只是错误处理程序中的一个问题。

1 个答案:

答案 0 :(得分:1)

返回JSON对象而非JSONResult,如下所示:

return Json(new { error = "xxxxxxxxxx"},JsonRequestBehavior.AllowGet);

查看this article

相关问题