Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem was caused by upgrading from jQuery 1.8 to 1.9. In jQuery 1.7 and 1.8, this in MVC:</p> <pre><code>return Json(null); </code></pre> <p>was accepted as valid JSON and interpreted as null. Technically, this sends a blank string back to the client with HTTP 200, and that's good enough for jQuery &lt;1.9.</p> <p>But now (we're using jQuery 1.9.1), it attempts to parse the empty string as JSON, jQuery's JSON parser throws an exception on empty string, and that triggers a code chain that ends in a <code>fail()</code> callback instead.</p> <p>A workaround is to instead pass this back from the server on success with no other information:</p> <pre><code>return Json(new{}); </code></pre> <p>That passes muster with jQuery's JSON parser and all is well. This also works:</p> <pre><code>return Json(true); </code></pre> <p><br/></p> <h1>Update</h1> <p>Musa notes below this behavior by MVC seems broken. This <a href="https://stackoverflow.com/questions/7109967/using-json-net-as-default-json-serializer-in-asp-net-mvc-3-is-it-possible/7150912#7150912">separate Stack Overflow answer to <em>Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?</em></a> covers how to get MVC to return null for <code>Json(null)</code> - basically, use Json.NET instead of ASP.NET MVC's built-in JSON serializer. This is the solution I ultimately ended up using.</p> <p>You need to use a slightly modified version of that answer to fix this however - code is below. Basically, don't include the <code>if</code> statement checking for null before passing to serialize, or you're right back in the same predicament.</p> <p><br/></p> <h1>Update 2</h1> <p>The default implementation of ISO 8601 Dates in Json.NET breaks <a href="http://en.wikipedia.org/wiki/Internet_Explorer_9" rel="nofollow noreferrer">Internet&nbsp;Explorer&nbsp;9</a> and below when it attempts to parse it with <code>new Date(...)</code>. In other words, these parse fine in Internet&nbsp;Explorer&nbsp;9:</p> <pre><code>var date = new Date('2014-09-18T17:21:57.669'); var date = new Date('2014-09-18T17:21:57.600'); </code></pre> <p>But this throws an exception:</p> <pre><code>var date = new Date('2014-09-18T17:21:57.6'); </code></pre> <p>Internet&nbsp;Explorer&nbsp;9's Date() implementation can't cope with anything but exactly three millisecond places. To fix this you have to override the Json.NET date format to force it. Included in the code below.</p> <pre><code>public class JsonNetResult : JsonResult { public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); var response = context.HttpContext.Response; response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; var settings = new JsonSerializerSettings { Converters = new[] {new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK" }} }; var jsonSerializer = JsonSerializer.Create(settings); jsonSerializer.Serialize(response.Output, Data); } } </code></pre> <p>A Gist that demonstrates how to tie this into a BaseController:</p> <p><a href="https://gist.github.com/b9chris/6991b341e89bb0a4e6d801d02dfd7730" rel="nofollow noreferrer">https://gist.github.com/b9chris/6991b341e89bb0a4e6d801d02dfd7730</a></p>
 

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