json iconie iconprototype icon

Prototype - X-JSON fails on long value in IE

Posted in , , , Thu, 01 Jun 2006 20:14:00 GMT

Unlike Dojo Toolkit and other client-side AJAX libraries that read a JSON object from the response body, prototype.js reads it from the custom X-JSON header. I didn't run into any problems with prototype's approach developing on Firefox but I soon discovered IE 6 has a max header size that will cause prototype 1.5.0 rc0's evalJSON method to silently fail with an unreported [object Error] error. In my case, IE 6 would handle an X-JSON string with length of 138 bytes but would fail with a length of 1659 bytes. Firefox 1.5.0.3 and 1.0.7 worked fine with both strings. I'm not sure what IE 6's exact limit is but the fact there's a limit at all is discouraging IMO.

The argument I've seen for putting the JSON object in X-JSON is that you can put a separate large quanity of HTML in the response body. This design works well when you only have one or zero large quantities of HTML. As soon as you have two, you need to either put them both in the response body using another serialized data notation or put them both in the JSON object and put the JSON object in the response body instead of the X-JSON header. It seems better to simply expect the JSON object in the response body in all cases than in cases when there's only one or zero large return values.

Let's take a look at an example. In the following the one Ajax JSON response can return meta data about the overall response ("Result":"ok") as well as a list of "Posts", say when updating a post list on something like Digg's homepage. Further more a list of "Hot Stories" can simultaneously updated in the same request. Each one of the stories may have a description or overall information that exceeds IE 6's max header length.

{
  "Result":"ok",
  "Posts":[
    {
      "time":1149349580,
      "id":100001,
      "title":"Article 11 title",
      "desc":"Article 11 desc
         (longer than IE header length)",
      "votes":120,
      "comments":5,
      "submitter":"garfield"
    },
    {
      "time":1149349583,
      "id":100002,
      "title":"Article 12 title",
      "desc":"Article 12 desc
         (longer than IE header length)",
      "votes":125,
      "comments":15,
      "submitter":"garfield"
    }
  ],
  "Hot Stories":[
    {
      "time":1149348000,
      "id":1001,
      "title":"Hot Article 1 title",
      "desc":"Hot Article 1 desc
         (longer than IE header length)",
      "votes":1120,
      "comments":115,
      "submitter":"garfield"
    },
    {
      "time":1149349000,
      "id":1002,
      "title":"Hot Article 2 title",
      "desc":"Hot Article 1 desc
         (longer than IE header length)",
      "votes":1120,
      "comments":115,
      "submitter":"garfield"
    }
  ]
}

I understand where having a proprietary X-JSON header and a separate response body is useful however I view those situations as a subset of a more generic, and powerful, use. With more complex return values, putting the JSON object in the response body is ultimately a more flexible and extensible design.

I'll continue to use prototype for now but I'm going to set all my JSON objects in the response body and eval it myself given IE 6's short comings. A bigger concern for me is that I feel Prototype's use of putting the JSON object in X-JSON will encourage inefficient AJAX implementations that end up using more HTTP request/response cycles than necessary. I really hope prototype.js will move their auto-eval feature to operate on the response body due to this problem and to make it more compatible with other JS AJAX libraries at the same time.

del.icio.us:Prototype - X-JSON fails on long value in IE digg:Prototype - X-JSON fails on long value in IE reddit:Prototype - X-JSON fails on long value in IE spurl:Prototype - X-JSON fails on long value in IE wists:Prototype - X-JSON fails on long value in IE simpy:Prototype - X-JSON fails on long value in IE newsvine:Prototype - X-JSON fails on long value in IE blinklist:Prototype - X-JSON fails on long value in IE furl:Prototype - X-JSON fails on long value in IE fark:Prototype - X-JSON fails on long value in IE blogmarks:Prototype - X-JSON fails on long value in IE Y!:Prototype - X-JSON fails on long value in IE smarking:Prototype - X-JSON fails on long value in IE magnolia:Prototype - X-JSON fails on long value in IE segnalo:Prototype - X-JSON fails on long value in IE

8 comments

Comments

  1. nurey.blogspot.com said about 1 month later:

    > I really hope prototype.js will move their auto-eval feature to operate on the response body due to this problem and to make it more compatible with other JS AJAX libraries at the same time.

    Amen!

  2. nu said 4 months later:

    I have exactly the same problem but in Firefox 1.5.0.7… what it might mean it’s not really a browser problem. I can receive correctly a X-JSON header up to 10227 characters. If longer it crashes silently… I agree nurey.blogspot.com when he says prototype.js should be modified to read post content.

  3. rich said 5 months later:

    Yep, v. frustrating, not least the silent failing that makes you tripple check all your other code!

  4. David Eads said 5 months later:

    Stumbled across this because I’m running into this problem. Yeah, this is a major bummer. Prototype seems significantly nicer and better designed than Dojo and others, but all I can say is arggggh. I have a project where JSON is stunningly obvious as the better choice for data interchange, but…argh!

  5. blogger news blog said 10 months later:

    I searched a long time for such an great article. Thank you very much. You have to be patient with such material if you’re really interested in extracting some info off.

  6. Phil said about 1 year later:

    Good news for you guys! :-)

    “Ajax.Request JSON support has been considerably improved in Prototype 1.6.0:

    • You can now access JSON response bodies as JavaScript objects using the transport object’s responseJSON property. (JSON responses must have a Content-type header of application/json.)”

    http://www.prototypejs.org/2007/8/15/prototype-1-6-0-release-candidate

  7. Vladimir said about 1 year later:

    I’m looking at prototype 1.6.0 right now, being caught in this JSON problem, and yep, the bug is still there.

    I’m using gzip, and the content has about 800bytes compressed. Uncompressed, it has about 3kb, and the file is sent as application/json.

    Guess what, FF likes it, I’m catching a JSON file using XMLHTTPRequest, using the responseJSON method, and IE doesn’t like.

    So far, I tried removing GZIP, trying using responseText, nothing worked.

    I’m still trying to figure out what’s wrong, so if there’s a solution to this, I’d be happy to see it, and stop wasting time reviewing prototype’s content :D

    Thanks for poiting the content size limit issue John.

    Vladimir Ghetau

  8. Vladimir said about 1 year later:

    Ignore my previous post, I had a syntax issue inside my code.

    The JSON string is parsed corectly by IE, and it isn’t sent using X-JSON header, I’m sending it as page content.

    When using the prototype.js libarary responseJSON, you’ll have to make sure the content of the page is application/json though, just as Phil said.

    Cheers!

    Vladimir Ghetau

(leave url/email »)

   Comment Markup Help Preview comment