简体   繁体   中英

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. The controller action type is 'JsonResult' and it is returning json data which should indicate success for the ajax call. But the ajax call is not responding for success or error.

 $.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

When we return JSONResult we get json object in the callback, so we don't need to Parse it. 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
           }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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