Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery post JSON fails when returning null from ASP.NET MVC
    primarykey
    data
    text
    <p>I use <a href="http://en.wikipedia.org/wiki/ASP.NET_MVC_Framework" rel="noreferrer">ASP.NET MVC</a> to post JSON from jQuery, and get some JSON back, using this little library function:</p> <pre><code>(function($) { $.postJson = function(url, data) { return $.ajax({ url: url, data: JSON.stringify(data), type: 'POST', dataType: 'json', contentType: 'application/json; charset=utf-8' }); }; })(jQuery); </code></pre> <p>So obviously I'll call this like:</p> <pre><code>$('#button').click(function() { $.postJson('/controller/action', { Prop1: 'hi', Prop2: 'bye' }) .done(function(r) { alert('It worked.'); }) .fail(function(x) { alert('Fail! ' + x.status); }); }); </code></pre> <p>ASP.NET MVC 3 and ASP.NET MVC 4 support the submit side of things (before that you needed to extend ASP.NET MVC to handle submitting JSON), but the problem I'm running into is on the return. On the Controller I often return null to basically say "Success, nothing else to say," like:</p> <pre><code>[HttpPost] public JsonResult DoSomething(string Prop1, string Prop2) { if (doSomething(Prop1, Prop2) return Json(null); // Success return Json(new { Message = "It didn't work for the following reasons" }); } </code></pre> <p>I use this pattern frequently and it works fine - my success/done callback gets called and all is well. But recently I upgraded ASP.NET MVC and jQuery, and it's stopped working - instead my fail callback is getting called whenever I <code>return Json(null);</code>. Furthermore, I've inspected the response and the statusCode getting returned is in fact 200, so the server isn't failing - jQuery's just saying it is.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload