简体   繁体   English

为什么虽然控制器发送数据,但ajax调用未返回成功或错误?

[英]Why ajax call not returning success or error though controller sends data?

I have an Ajax call of 'GET' type which is hitting the mvc controller action properly. 我有一个'GET'类型的Ajax调用,它正确地击中了mvc控制器动作。 The controller action type is 'JsonResult' and it is returning json data which should indicate success for the ajax call. 控制器操作类型为“ JsonResult”,它返回的json数据应表明ajax调用成功。 But the ajax call is not responding for success or error. 但是ajax调用不会响应成功或错误。

 $.ajax({
    url: baseUrl + '/Controller_Name/Action_Name',
    type: 'GET',
    data: param,
    success: function (data) {
        var response = JSON.parse(data);
        if (response.length > 0 && response != '-1') {

            toastr.options.timeOut = 2500;
            toastr.success('Data retrieved successfully', 'Success');

        }
    },
    error: function (xhr) {

        toastr.options.timeOut = 2500;
        toastr.warning('Error while retrieving data', 'Error');
    }
});

The controller action type is 'JsonResult' and it is returning json data which should indicate success for the ajax call 控制器操作类型为“ JsonResult”,它返回的json数据应表明ajax调用成功

When we return JSONResult we get json object in the callback, so we don't need to Parse it. 当我们返回JSONResult我们在回调中获得json对象,因此我们不需要解析它。 If the action returns like following: 如果操作返回如下:

public ActionResult YourAction()
{
    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

then in success the following should work: 那么以下内容应该可以成功工作:

success: function (data) {
           alert(data.success);
           if(data.success === true) {
                // do something here
           }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM