Json responseText为空,statusText为错误

时间:2013-11-02 21:24:14

标签: json jquery jsonp jsonresult

从mvc 4行动:

    [HttpPost]
    public ActionResult DoStuff(string str)
    {
        // Do some things

            Response.ContentType = "application/json;charset=utf-8";
            Response.StatusCode = someCondition == true ? HttpStatusCode.OK : HttpStatusCode.NotFound);
            Response.TrySkipIisCustomErrors = true;

        return Json(
            new {
               object1 = 1,
               object2 = someArray[0],
               object3 = someArray[1],
               object4 = someValue == 4 ? 1 : 0
            }
        );
    }

在jquery ajax中:

ajax({
    url: '/Ctrler/DoStuff/',
    data: { str: someString },
    type: 'POST',
    dataType: 'json'
}).then(function (data) {
    var _response = $.parseJSON(data);
}, function (data) {
    var _response = $.parseJSON(data.responseText);
});

data.responseText为空(“”),statusText为“error”。它并不总是发生。我观察到它是随机发生的。为什么呢?

1 个答案:

答案 0 :(得分:0)

您的数据已经从JSON转换为对象,因此您无需再次解析它。试试这个:

ajax({
    url: '/Ctrler/DoStuff/',
    data: { str: someString },
    type: 'POST',
    dataType: 'json'
}).then(function (data) {
    // use data here...
    alert(data.object1);
}, function () {
    alert('request failed');
});
相关问题