Empty Charsets in IE 9 Causing JSON Parsing Errors

Monday, October 1 2011 javascript

My last post was a bit of a pulpy, prosey trope about a problem I had debugging an issue in IE 9. It was supposed to be a fun read - but it turns out I might have actually uncovered an issue with IE 9... so giving the team due respect, here's a bit more detail.

##The Spec

The core of the issue here is that IE 9 is strict. Very strict. And that's generally a good thing - unless the spec is a bit too vague. My problem yesterday had to do with the spec, specifically the charset setting of the Content Type:


Is this wrong? You tell me -

here's the spec:


The question is this:

is the charset null? Or is it invalid?


You can reproduce this quite easily using .NET. You'll need two pages - index.cshtml:

Hi Everyone

Output from JSON:

And then "json.cshtml":

@{ Response.ContentType = "application/json; charset="; } {"message":"Charset Issue"}

You can do this in Rails by setting "content_type" of render manually:

render :json => {:message => "hello there"}.tojson, :contenttype => "application/json, charset="

Load the page up in Chrome, Safari, or Firefox and it works. Load it up in IE 9 and you'll get an alert with a very, very weird error:


Thanks Jon!

Credit for this find goes to

Jon Galloway who read the spec after I called him names on Twitter :). He found some inconsistent behavior with IE with his reading of the spec (mine too) and has forwarded it off to the IE 9 team.

When I wrote my post yesterday - it was with the understanding that it was my fault :) - but it occurred to me that

if it wasn't my fault,

then I probably went about things a bit poorly. So hopefully this post is helpful.