0

I have a controller that returns the following line:

Json(null, JsonRequestBehavior.AllowGet);

This method in the controller is executed by an Ajax request like this:

return new Promise (function(resolve, reject)
            { $.ajax({
                type: 'POST',
                url: GL_ROOT + "AccountingCompany/DeleteAccountingCompany",
                data: JSON.stringify({ id: id }),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (deleteResult) {
                    resolve(deleteResult);
                }
            });
        });

When I set a break-point in the success part, it never gets there. But if I changed the result from the controller to Json(-1, JsonRequestBehavior.AllowGet); it gets to the break-point in the success part of the Ajax request.

Can somebody know the reason why I can not reach the break-point?.

  • 1
    According to the [Controller.Json() MSDN](https://msdn.microsoft.com/en-us/library/ee430920(v=vs.118).aspx) the `data` parameter must be a serializable value, I don't have much experience with C# but my guess is that `null` isn't serializable causing the request to fail, have you tried checking the network tab in dev tools to see if the request is a failure? or defining a failure handler and breakpointing there too? – Patrick Barr Jun 28 '17 at 17:57
  • Looks like there is a way to ignore nulls https://stackoverflow.com/questions/9819640/ignoring-null-fields-in-json-net – Hopeless Jun 28 '17 at 17:59
  • The request executes correctly, looks like in fact `null` isn't serializable and that's why it can get to the break-point. –  Jun 28 '17 at 20:44

1 Answers1

0

There might be parsing issues in client side when trying to resolve the out come of Json(null), which translates to an empty string. Try using Json(new {}, JsonRequestBehavior.AllowGet) instead.

Hope it helps.

Wail
  • 137
  • 8
  • thanks for your help, it gets to the break-point, but I cannot compare the result to null like this `result == null` or `result == ' ' ` –  Jun 28 '17 at 20:47
  • you're welcome Alejandro, there are multiple ways to test equality, and here are two of them: if you are using jQuery, try `jQuery.isEmptyObject(result);`, if not try `JSON.stringify(result) === JSON.stringify({});` – Wail Jun 29 '17 at 10:28