0

从 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"。它并不总是发生。我观察到它是随机发生的。为什么?

4

1 回答 1

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');
});
于 2013-11-02T21:35:14.340 回答