<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Dev411 Blog: Category json</title>
  <subtitle type="html">John Wang on Technology</subtitle>
  <id>tag:www.dev411.com,2005:Typo</id>
  <generator uri="http://www.typosphere.org" version="4.0">Typo</generator>
  <link href="http://www.dev411.com/blog/xml/atom/category/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.dev411.com/blog/tag/json" rel="alternate" type="text/html"/>
  <updated>2007-06-16T12:30:23-05:00</updated>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:885f059fcc2a7604158abbca1b8b8be6</id>
    <published>2006-07-25T10:49:00-05:00</published>
    <updated>2007-06-16T12:30:23-05:00</updated>
    <title type="html">Catalyst - Customizing the view to stop IE from caching JSON</title>
    <link href="http://www.dev411.com/blog/2006/07/25/catalyst-customizing-the-view-stop-ie-from-caching-json" rel="alternate" type="text/html"/>
    <category term="json" scheme="http://www.dev411.com/blog/tag/json" label="json"/>
    <category term="catalyst" scheme="http://www.dev411.com/blog/tag/catalyst" label="catalyst"/>
    <category term="ie" scheme="http://www.dev411.com/blog/tag/ie" label="ie"/>
    <category term="ajax" scheme="http://www.dev411.com/blog/tag/ajax" label="ajax"/>
    <summary type="html">&lt;p&gt;Often times you'll want to customize your response specific to the View being used, e.g. setting extra headers. This can be done directly in your View class by creating a &lt;span class="fix"&gt;process&lt;/span&gt; method. All View base classes have a process method defined in &lt;span class="fix"&gt;Catalyst::View&lt;/span&gt; that gets called at rendering time. By adding a &lt;span class="fix"&gt;process&lt;/span&gt; method in your subclass and redispatching to the parent you can do some preliminary processing.&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;Often times you'll want to customize your response specific to the View being used, e.g. setting extra headers. This can be done directly in your View class by creating a &lt;span class="fix"&gt;process&lt;/span&gt; method. All View base classes have a process method defined in &lt;span class="fix"&gt;Catalyst::View&lt;/span&gt; that gets called at rendering time. By adding a &lt;span class="fix"&gt;process&lt;/span&gt; method in your subclass and redispatching to the parent you can do some preliminary processing.&lt;/p&gt;

&lt;p&gt;One use is preventing IE from caching JSON responses by setting the Cache-Control and Pragma HTTP response headers. You can do this in the 
application's JSON view as follows:&lt;/p&gt;

&lt;pre&gt;package MyApp::View::JSON;
use strict; use warnings;
use base qw/Catalyst::View::JSON/;

sub process {
  my $self = shift;
  my $c = $_[0];
  $c-&gt;res-&gt;header( 'Cache-Control' =&gt;
    'no-store, no-cache, must-revalidate,'.
    'post-check=0, pre-check=0, max-age=0'
  );
  $c-&gt;res-&gt;header( 'Pragma' =&gt; 'no-cache' );
  $c-&gt;res-&gt;header( 'Expires' =&gt; 'Thu, 01 Jan 1970 00:00:00 GMT' );
  $self-&gt;NEXT::process(@_);
}

1;&lt;/pre&gt;

&lt;p&gt;Your &lt;span class="fix"&gt;process&lt;/span&gt; method will get called, set it's response headers and then use &lt;a href="http://search.cpan.org/~dconway/NEXT-0.60/lib/NEXT.pm" class="fix"&gt;NEXT&lt;/a&gt; to call C::V::JSON's &lt;span class="fix"&gt;process&lt;/span&gt; method.&lt;/p&gt;</content>
  </entry>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:310f8f2393192a4bf6885b036d510d30</id>
    <published>2006-07-24T20:40:00-05:00</published>
    <updated>2007-06-16T12:30:23-05:00</updated>
    <title type="html">JSON XSS exploit: don't use text/html</title>
    <link href="http://www.dev411.com/blog/2006/07/24/json-xss-exploit-dont-use-text-html" rel="alternate" type="text/html"/>
    <category term="json" scheme="http://www.dev411.com/blog/tag/json" label="json"/>
    <category term="catalyst" scheme="http://www.dev411.com/blog/tag/catalyst" label="catalyst"/>
    <category term="xss" scheme="http://www.dev411.com/blog/tag/xss" label="xss"/>
    <category term="security" scheme="http://www.dev411.com/blog/tag/security" label="security"/>
    <category term="dojo" scheme="http://www.dev411.com/blog/tag/dojo" label="dojo"/>
    <category term="ajax" scheme="http://www.dev411.com/blog/tag/ajax" label="ajax"/>
    <summary type="html">&lt;p&gt;&lt;a href="http://jibbering.com/blog/?p=514"&gt;Jim Ley reports&lt;/a&gt; on the &lt;a href="http://ha.ckers.org/blog/20060704/cross-site-scripting-vulnerability-in-google/"&gt;Google JSON XSS exploit&lt;/a&gt; with example code and screen shots of stealing information from the AdSense site. The moral of the story is don't use &lt;span class="fix"&gt;text/html&lt;/span&gt; for the MIME type when returning JSON, use &lt;span class="fix"&gt;application/json&lt;/span&gt; which is an &lt;a href="http://www.rfc-editor.org/rfc/rfc4627.txt"&gt;IETF standard (RFC 4627)&lt;/a&gt; now. Most browsers should handle &lt;span class="fix"&gt;application/json&lt;/span&gt; fine, however Opera may have problems and you may want to use &lt;span class="fix"&gt;application/x-javascript&lt;/span&gt; for that. Something to remember even if your AJAX code/library doesn't care about the MIME type returned by the server, e.g. Dojo.&lt;/p&gt;

&lt;p&gt;If you are using Catalyst and Catalyst::View::JSON, your JSON response will automatically be set to &lt;span class="fix"&gt;application/json&lt;/span&gt; for all user agents except Opera (which gets &lt;span class="fix"&gt;application/x-javascript&lt;/span&gt;) so you're already safe(r).&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;&lt;a href="http://jibbering.com/blog/?p=514"&gt;Jim Ley reports&lt;/a&gt; on the &lt;a href="http://ha.ckers.org/blog/20060704/cross-site-scripting-vulnerability-in-google/"&gt;Google JSON XSS exploit&lt;/a&gt; with example code and screen shots of stealing information from the AdSense site. The moral of the story is don't use &lt;span class="fix"&gt;text/html&lt;/span&gt; for the MIME type when returning JSON, use &lt;span class="fix"&gt;application/json&lt;/span&gt; which is an &lt;a href="http://www.rfc-editor.org/rfc/rfc4627.txt"&gt;IETF standard (RFC 4627)&lt;/a&gt; now. Most browsers should handle &lt;span class="fix"&gt;application/json&lt;/span&gt; fine, however Opera may have problems and you may want to use &lt;span class="fix"&gt;application/x-javascript&lt;/span&gt; for that. Something to remember even if your AJAX code/library doesn't care about the MIME type returned by the server, e.g. Dojo.&lt;/p&gt;

&lt;p&gt;If you are using Catalyst and Catalyst::View::JSON, your JSON response will automatically be set to &lt;span class="fix"&gt;application/json&lt;/span&gt; for all user agents except Opera (which gets &lt;span class="fix"&gt;application/x-javascript&lt;/span&gt;) so you're already safe(r).&lt;/p&gt;

</content>
  </entry>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:c3c0a3e7d0f087a112bdef013edeea1b</id>
    <published>2006-06-03T23:15:00-05:00</published>
    <updated>2007-06-16T12:30:22-05:00</updated>
    <title type="html">Dojo - Using dojo.io.bind</title>
    <link href="http://www.dev411.com/blog/2006/06/03/dojo-using-dojo-io-bind" rel="alternate" type="text/html"/>
    <category term="json" scheme="http://www.dev411.com/blog/tag/json" label="json"/>
    <category term="dojo" scheme="http://www.dev411.com/blog/tag/dojo" label="dojo"/>
    <category term="ajax" scheme="http://www.dev411.com/blog/tag/ajax" label="ajax"/>
    <summary type="html">&lt;p&gt;I recently looked into Dojo Toolkit 0.3.0's dojo.io.bind to perform AJAX requests because of it's &lt;a href="http://alex.dojotoolkit.org/?p=528"&gt;good handling of memory leaks&lt;/a&gt;. I checked out the &lt;a href="http://dojotoolkit.org/intro_to_dojo_io.html"&gt;dojotoolkit.org intro page&lt;/a&gt; which was okay but didn't cover how to pass parameters or how to process a JSON response so I thought I'd put some info together here. Here are a few things worth knowing:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;A dojo response has a &lt;span class="fix"&gt;type&lt;/span&gt; that is typically set to &lt;span class="fix"&gt;load&lt;/span&gt; or &lt;span class="fix"&gt;error&lt;/span&gt;, though sometimes it may be neither. With dojo, &lt;span class="fix"&gt;type=="load"&lt;/span&gt; means success.&lt;/li&gt;&lt;li&gt;Dojo.io.bind has a few handlers to process the response. The &lt;span class="fix"&gt;load&lt;/span&gt; handler is executed when the response &lt;span class="fix"&gt;type=="load"&lt;/span&gt; and the &lt;span class="fix"&gt;error&lt;/span&gt; handler is executed when the response &lt;span class="fix"&gt;type=="error"&lt;/span&gt;. There is also a catch-all &lt;span class="fix"&gt;handle&lt;/span&gt; handler that can accept all response types so you can process a response that has a type that's neither load nor error.&lt;/li&gt;&lt;li&gt;If request parameters are set as a hashref using &lt;span class="fix"&gt;content&lt;/span&gt;, dojo.io.bind will automatically turn them into form post parameters&lt;/li&gt;&lt;li&gt;The response &lt;span class="fix"&gt;mimetype&lt;/span&gt; (or &lt;span class="fix"&gt;content-type&lt;/span&gt;) is set in the client JavaScript. Dojo uses this to determine how to process the response. Dojo ignores content-types set by the server&lt;/li&gt;&lt;/ol&gt;

&lt;p&gt;Here are two examples to help put the above together. The examples cover a plain HTML fragment response as well as a JSON response. Both examples submit a hashref. Dojo can also submit an entire form automatically. This is covered on the Dojo.IO Intro link above and isn't covered here, for now. Thanks to Toby Corkindale who posted &lt;a href="http://lists.rawmode.org/pipermail/catalyst/2006-June/007891.html"&gt;his saveHandler function to the catalyst mailing list&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1: HTML processed by load&lt;/strong&gt;&lt;br /&gt;This first example retrieves a single fragment of HTML in the response body and processes it using the load handler.&lt;/p&gt;

&lt;pre&gt;dojo.io.bind({
    url: "/binduri",
    method: "post",
    /* optional content. If the content is in the
     * form of a hashref they are converted to
     * post paramters */
    content: {
        username: "George",
        password: "curious"
    },
    load: function(type,data,evt) {
        if (data) alert('Response Data: '+data);
    },
    mimetype:'text/html'
});&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Example 2: JSON processed by handle&lt;/strong&gt;&lt;br /&gt;This second example processes a JSON response using the all-in-one &lt;span class="fix"&gt;handle&lt;/span&gt; handler. The &lt;span class="fix"&gt;handle&lt;/span&gt; is typically used in special situations where &lt;span class="fix"&gt;load&lt;/span&gt; and &lt;span class="fix"&gt;error&lt;/span&gt; are not appropriate such as a progress bar. It is shown here just to provide the syntax. To process a JSON response, set the &lt;span class="fix"&gt;dojo.io.bind mimetype&lt;/span&gt; to &lt;span class="fix"&gt;text/json&lt;/span&gt; and return the JSON object in the response body.&lt;/p&gt;

&lt;pre&gt;dojo.io.bind({
    url: "/binduri",
    method: "post",
    /* optional content. If the content is in the
     * form of a hashref they are converted to
     * post paramters */
    content: {
        username: "George",
        password: "curious"
    },
    handle: function(type,data,evt) {
        if (type=='load') {
            alert( 'Successful response' );
            if (data.myItem)
                alert('Response JSON Item: '
                +data.myItem);
        } else if (type=='error') {
            alert('A error has occurred');
        } else {
            alert('Some other event has occurred');
        }
    },
    mimetype:'text/json'
});&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;Dojo.io has a nice interface that has a few conveniences including auto-conversion of a hashref to request form parameters and auto-eval of JSON set in the response body. Along with the good memory leak handling for IE, a very nice implementation.&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;I recently looked into Dojo Toolkit 0.3.0's dojo.io.bind to perform AJAX requests because of it's &lt;a href="http://alex.dojotoolkit.org/?p=528"&gt;good handling of memory leaks&lt;/a&gt;. I checked out the &lt;a href="http://dojotoolkit.org/intro_to_dojo_io.html"&gt;dojotoolkit.org intro page&lt;/a&gt; which was okay but didn't cover how to pass parameters or how to process a JSON response so I thought I'd put some info together here. Here are a few things worth knowing:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;A dojo response has a &lt;span class="fix"&gt;type&lt;/span&gt; that is typically set to &lt;span class="fix"&gt;load&lt;/span&gt; or &lt;span class="fix"&gt;error&lt;/span&gt;, though sometimes it may be neither. With dojo, &lt;span class="fix"&gt;type=="load"&lt;/span&gt; means success.&lt;/li&gt;&lt;li&gt;Dojo.io.bind has a few handlers to process the response. The &lt;span class="fix"&gt;load&lt;/span&gt; handler is executed when the response &lt;span class="fix"&gt;type=="load"&lt;/span&gt; and the &lt;span class="fix"&gt;error&lt;/span&gt; handler is executed when the response &lt;span class="fix"&gt;type=="error"&lt;/span&gt;. There is also a catch-all &lt;span class="fix"&gt;handle&lt;/span&gt; handler that can accept all response types so you can process a response that has a type that's neither load nor error.&lt;/li&gt;&lt;li&gt;If request parameters are set as a hashref using &lt;span class="fix"&gt;content&lt;/span&gt;, dojo.io.bind will automatically turn them into form post parameters&lt;/li&gt;&lt;li&gt;The response &lt;span class="fix"&gt;mimetype&lt;/span&gt; (or &lt;span class="fix"&gt;content-type&lt;/span&gt;) is set in the client JavaScript. Dojo uses this to determine how to process the response. Dojo ignores content-types set by the server&lt;/li&gt;&lt;/ol&gt;

&lt;p&gt;Here are two examples to help put the above together. The examples cover a plain HTML fragment response as well as a JSON response. Both examples submit a hashref. Dojo can also submit an entire form automatically. This is covered on the Dojo.IO Intro link above and isn't covered here, for now. Thanks to Toby Corkindale who posted &lt;a href="http://lists.rawmode.org/pipermail/catalyst/2006-June/007891.html"&gt;his saveHandler function to the catalyst mailing list&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1: HTML processed by load&lt;/strong&gt;&lt;br /&gt;This first example retrieves a single fragment of HTML in the response body and processes it using the load handler.&lt;/p&gt;

&lt;pre&gt;dojo.io.bind({
    url: "/binduri",
    method: "post",
    /* optional content. If the content is in the
     * form of a hashref they are converted to
     * post paramters */
    content: {
        username: "George",
        password: "curious"
    },
    load: function(type,data,evt) {
        if (data) alert('Response Data: '+data);
    },
    mimetype:'text/html'
});&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Example 2: JSON processed by handle&lt;/strong&gt;&lt;br /&gt;This second example processes a JSON response using the all-in-one &lt;span class="fix"&gt;handle&lt;/span&gt; handler. The &lt;span class="fix"&gt;handle&lt;/span&gt; is typically used in special situations where &lt;span class="fix"&gt;load&lt;/span&gt; and &lt;span class="fix"&gt;error&lt;/span&gt; are not appropriate such as a progress bar. It is shown here just to provide the syntax. To process a JSON response, set the &lt;span class="fix"&gt;dojo.io.bind mimetype&lt;/span&gt; to &lt;span class="fix"&gt;text/json&lt;/span&gt; and return the JSON object in the response body.&lt;/p&gt;

&lt;pre&gt;dojo.io.bind({
    url: "/binduri",
    method: "post",
    /* optional content. If the content is in the
     * form of a hashref they are converted to
     * post paramters */
    content: {
        username: "George",
        password: "curious"
    },
    handle: function(type,data,evt) {
        if (type=='load') {
            alert( 'Successful response' );
            if (data.myItem)
                alert('Response JSON Item: '
                +data.myItem);
        } else if (type=='error') {
            alert('A error has occurred');
        } else {
            alert('Some other event has occurred');
        }
    },
    mimetype:'text/json'
});&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;Dojo.io has a nice interface that has a few conveniences including auto-conversion of a hashref to request form parameters and auto-eval of JSON set in the response body. Along with the good memory leak handling for IE, a very nice implementation.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:5bd12f134d64f49b67f22215e9476f5f</id>
    <published>2006-06-01T15:14:00-05:00</published>
    <updated>2007-06-16T12:30:22-05:00</updated>
    <title type="html">Prototype - X-JSON fails on long value in IE</title>
    <link href="http://www.dev411.com/blog/2006/06/01/prototype-x-json-fails-on-long-value-in-ie" rel="alternate" type="text/html"/>
    <category term="json" scheme="http://www.dev411.com/blog/tag/json" label="json"/>
    <category term="ie" scheme="http://www.dev411.com/blog/tag/ie" label="ie"/>
    <category term="ajax" scheme="http://www.dev411.com/blog/tag/ajax" label="ajax"/>
    <category term="prototype" scheme="http://www.dev411.com/blog/tag/prototype" label="prototype"/>
    <summary type="html">&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;pre&gt;{
  "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"
    }
  ]
}&lt;/pre&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;pre&gt;{
  "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"
    }
  ]
}&lt;/pre&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</content>
  </entry>
  <entry>
    <author>
      <name>John Wang</name>
    </author>
    <id>urn:uuid:f9fb9eb8e9b9f0de3b18a6f89c2f0b45</id>
    <published>2006-05-31T19:42:00-05:00</published>
    <updated>2007-06-16T12:30:22-05:00</updated>
    <title type="html">Prototype, JSON and Catalyst</title>
    <link href="http://www.dev411.com/blog/2006/05/31/prototype-json-and-catalyst" rel="alternate" type="text/html"/>
    <category term="json" scheme="http://www.dev411.com/blog/tag/json" label="json"/>
    <category term="catalyst" scheme="http://www.dev411.com/blog/tag/catalyst" label="catalyst"/>
    <category term="perl" scheme="http://www.dev411.com/blog/tag/perl" label="perl"/>
    <category term="ajax" scheme="http://www.dev411.com/blog/tag/ajax" label="ajax"/>
    <category term="prototype" scheme="http://www.dev411.com/blog/tag/prototype" label="prototype"/>
    <summary type="html">&lt;p&gt;I use the prototype.js library (aka prototype) for AJAX and recently moved to using JSON to return multiple elements instead of a single one. I had to rearchitect my client request code and my server code to handle JSON but it's worth it to minimize the number of HTTP requests. I use Catalyst and JSON::Syck which make generating the JSON response on the server-side very easy.&lt;/p&gt;

&lt;p&gt;Prototype.js is interesting in that it auto-evals a JSON object that's placed in the X-JSON header instead of the response body like other libaries such as Dojo Toolkit. This allows you to send HTML in the response body but I'm not convinced this is a good implementation since it supports only one HTML fragment in the response body. I think it would be better to simply put the JSON object in the response body and have all the HTML fragments in the JSON object. I just seems cleaner to put all the HTML fragments in JSON instead of fragment 1 in the response body and fragments 2-n in JSON. Although I have the X-JSON header working, I may just move to the more standard JSON in response body approach and eval the JSON string myself. To read the auto-evaled JSON response, the following example expects a JSON associative array and displays the value of the myItem key (more code is available in the wiki link below):&lt;/p&gt;

&lt;pre&gt;onComplete: function( request, json ) {
    alert( json.myItem );
}&lt;/pre&gt;

&lt;p&gt;On the server-side, I use the Catalyst framework to set the header and JSON::Syck to transform Perl data structures to JSON. JSON::Syck is a wrapper around the C libsyck library and very fast. There is a view, Catalyst::View::JSON, however it puts the JSON string in the response body because that is what Dojo and other libraries expect. Since AJAX libraries don't idnetify themselves in anyway to the server (e.g. user agent), the Catalyst View would probably need a patch to with a configuration switch to populate X-JSON instead of the response body. For now, I'm using JSON::Syck directly. The following are the minimal calls to use. If you have UTF-8 output, see C::V::JSON for UTF-8 encoding.&lt;/p&gt;

&lt;pre&gt;$c-&gt;res-&gt;header(
    'X-JSON' =&gt; JSON::Syck::Dump( \%args );
);&lt;/pre&gt;

&lt;p&gt;I've put together some example code that I thought would be better placed on the wiki. It can be found at: &lt;a href="http://www.dev411.com/wiki/Prototype.js%2C_JSON_and_Catalyst"&gt;Prototype.js, JSON and Catalyst&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;UPDATE: After running into the &lt;a href="/blog/2006/06/01/prototype-x-json-fails-on-long-value-in-ie"&gt;IE max header length issue&lt;/a&gt;, I've decided to put the JSON object in the response body and manually eval request.responseText on the client.&lt;/p&gt;</summary>
    <content type="html">&lt;p&gt;I use the prototype.js library (aka prototype) for AJAX and recently moved to using JSON to return multiple elements instead of a single one. I had to rearchitect my client request code and my server code to handle JSON but it's worth it to minimize the number of HTTP requests. I use Catalyst and JSON::Syck which make generating the JSON response on the server-side very easy.&lt;/p&gt;

&lt;p&gt;Prototype.js is interesting in that it auto-evals a JSON object that's placed in the X-JSON header instead of the response body like other libaries such as Dojo Toolkit. This allows you to send HTML in the response body but I'm not convinced this is a good implementation since it supports only one HTML fragment in the response body. I think it would be better to simply put the JSON object in the response body and have all the HTML fragments in the JSON object. I just seems cleaner to put all the HTML fragments in JSON instead of fragment 1 in the response body and fragments 2-n in JSON. Although I have the X-JSON header working, I may just move to the more standard JSON in response body approach and eval the JSON string myself. To read the auto-evaled JSON response, the following example expects a JSON associative array and displays the value of the myItem key (more code is available in the wiki link below):&lt;/p&gt;

&lt;pre&gt;onComplete: function( request, json ) {
    alert( json.myItem );
}&lt;/pre&gt;

&lt;p&gt;On the server-side, I use the Catalyst framework to set the header and JSON::Syck to transform Perl data structures to JSON. JSON::Syck is a wrapper around the C libsyck library and very fast. There is a view, Catalyst::View::JSON, however it puts the JSON string in the response body because that is what Dojo and other libraries expect. Since AJAX libraries don't idnetify themselves in anyway to the server (e.g. user agent), the Catalyst View would probably need a patch to with a configuration switch to populate X-JSON instead of the response body. For now, I'm using JSON::Syck directly. The following are the minimal calls to use. If you have UTF-8 output, see C::V::JSON for UTF-8 encoding.&lt;/p&gt;

&lt;pre&gt;$c-&gt;res-&gt;header(
    'X-JSON' =&gt; JSON::Syck::Dump( \%args );
);&lt;/pre&gt;

&lt;p&gt;I've put together some example code that I thought would be better placed on the wiki. It can be found at: &lt;a href="http://www.dev411.com/wiki/Prototype.js%2C_JSON_and_Catalyst"&gt;Prototype.js, JSON and Catalyst&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;UPDATE: After running into the &lt;a href="/blog/2006/06/01/prototype-x-json-fails-on-long-value-in-ie"&gt;IE max header length issue&lt;/a&gt;, I've decided to put the JSON object in the response body and manually eval request.responseText on the client.&lt;/p&gt;

</content>
  </entry>
</feed>

