Empty Charsets in IE 9 Causing JSON Parsing Errors

##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:

/wp-content/uploads/ghost/json_charset.png

Is this wrong? You tell me –

here’s the spec:

/wp-content/uploads/ghost/json_charset3.png

The question is this:

is the charset null? Or is it invalid?

##Reproduction

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”}.to_json, :content_type => “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:

/wp-content/uploads/ghost/ie9_error.png

##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.

Tagged as: