0

Im using jQuery 1.9 with a few other libraries (kendo) and making an ajax call to an MVC controller. The controller method succeeds, but when there is no data to return (null), the ajax call does not enter the success block. Looking at other threads on this, I tried the following, but still it only falls into the success block if the call returns a non-null result.

My MVC controller returns a Jsonresult like this where retval is a collection of items

[update]
I think I am running into the issue mentioned here jQuery post JSON fails when returning null from ASP.NET MVC

    JsonResult j;
    if(retval != null)
    {
        j = Json(retval, JsonRequestBehavior.AllowGet);
    }
    else
    {
        j = Json(new { }, JsonRequestBehavior.AllowGet);
    }

    return j;
Community
  • 1
  • 1
bitshift
  • 6,026
  • 11
  • 44
  • 108
  • 1
    1) Please provide client side code which is making the call. 2) Also, the server code is not complete. You need to provide controller and action definition. Your call will fail if required parameters int,decimal etc.. not provided. 3) Also, log exception block error to console (stringified if required) and provide that too.. All this will help you find and answer fast.. – Ashokkumar M. Prajapati Oct 21 '16 at 01:46
  • Possible duplicate of [jQuery post JSON fails when returning null from ASP.NET MVC](http://stackoverflow.com/questions/15939944/jquery-post-json-fails-when-returning-null-from-asp-net-mvc) – Joel Harkes Oct 21 '16 at 04:16
  • Somewhat related to [this question](http://stackoverflow.com/questions/15939944/jquery-post-json-fails-when-returning-null-from-asp-net-mvc). If you client side code is using `$.ajax()` and you remove the `dataType` option, it will hit the success block –  Oct 23 '16 at 03:55

1 Answers1

0

jQuery cannot parse null as json. Return an empty object or even true as json.

Ali Kareem Raja
  • 566
  • 1
  • 7
  • 21