Hi, please consider this stripped down example: new Ajax.Request(''foo.php'', { onSuccess: function(t, json) { alert(json.myParam); } }); According to http://www.sergiopereira.com/articles/prototype.js.html, the second param is supposed to be the evaluated json object IF the X-JSON header is send in the response. I send the header in my script. However, the json var is always undefined and i have to evaluate t.responseText myself. Am i missing anything important? Best Regards, Dirk Eschler -- Dirk Eschler <mailto:dirk.eschler-hi6Y0CQ0nG0@public.gmane.org> http://www.krusader.org
On 5/28/06, Dirk Eschler <dirk.eschler-hi6Y0CQ0nG0@public.gmane.org> wrote:> > According to http://www.sergiopereira.com/articles/prototype.js.html, the > second param is supposed to be the evaluated json object IF the X-JSON > header > is send in the response. I send the header in my script. However, the json > var is always undefined and i have to evaluate t.responseText myself. > > Am i missing anything important?What version of prototype are you using? I think you need to be running 1.4.0 or higher. I''m using 1.5.0 rc0 and auto-eval of X-JSON works. It''d be nice if the libraries could agree on how to accept the JSON response since Dojo and other libraries seem to auto-eval responseText. This is an issue if you have a generic server-side JSON library since there doesn''t seem to be a way to identify the library being used (e.g. user agent). John _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Am Sonntag, 28. Mai 2006 17:17 schrieb John Wang:> What version of prototype are you using? I think you need to be running > 1.4.0 or higher. I''m using 1.5.0 rc0 and auto-eval of X-JSON works.[...] Hi John, i''m using 1.5.0-rc0 too. So the problem must be in my PHP script. How do you send the header? I''ve tried the below content types followed by a simple echo() of the json string. header("Content-Type: X-JSON"); header("Content-Type: text/x-json"); header("Content-Type: application/x-json"); -- Dirk Eschler <mailto:dirk.eschler-hi6Y0CQ0nG0@public.gmane.org> http://www.krusader.org
Try: header(''X-JSON: {some:1, json:2, string:3}''); :-) On Sun, 2006-05-28 at 17:37 +0200, Dirk Eschler wrote:> > header("Content-Type: X-JSON"); > header("Content-Type: text/x-json"); > header("Content-Type: application/x-json"); >
X-JSON is the name of the custom header, not a content-type so header("Content-Type: X-JSON") won''t work. You need the X-JSON header set to the JSON string. In PHP, do the following: header("X-JSON: $json"); I use Catalyst with the following. It''s pretty similar to the PHP code so I expect that to work for you. $c->res->headers->header( ''X-JSON'' => $json ); On a side note. Is X-JSON a standard or just for prototype? The JSON response can be pretty large. Is stuffing a header a good implementation? HTH, John _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Personally i loved the ''json in header'' feature, it allowed me to have large quantities of html in the responce body, and some params, or other divs to update, etc in the x-json header .. Worked out much better then having to xml parse the responce body into sepperate entities :-) (like openrico.org does forinstance) -- Chris On Sun, 2006-05-28 at 08:48 -0700, John Wang wrote:> X-JSON is the name of the custom header, not a content-type so > header("Content-Type: X-JSON") won''t work. You need the X-JSON header > set to the JSON string. In PHP, do the following: > > header("X-JSON: $json"); > > I use Catalyst with the following. It''s pretty similar to the PHP code > so I expect that to work for you. > > $c->res->headers->header( ''X-JSON'' => $json ); > > On a side note. Is X-JSON a standard or just for prototype? The JSON > response can be pretty large. Is stuffing a header a good > implementation? > > HTH, > John > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Am Sonntag, 28. Mai 2006 17:48 schrieb John Wang:> X-JSON is the name of the custom header, not a content-type so > header("Content-Type: X-JSON") won''t work. You need the X-JSON header set > to the JSON string. In PHP, do the following: > > header("X-JSON: $json"); > > I use Catalyst with the following. It''s pretty similar to the PHP code so I > expect that to work for you. > > $c->res->headers->header( ''X-JSON'' => $json ); > > On a side note. Is X-JSON a standard or just for prototype? The JSON > response can be pretty large. Is stuffing a header a good implementation?Thanks guys, the custom header approach works. I''ve used the PHP script with Dojo before. As far as i remember they also supported the application/x-json content type, that kinda confused me. Best Regards, Dirk Eschler -- Dirk Eschler <mailto:dirk.eschler-hi6Y0CQ0nG0@public.gmane.org> http://www.krusader.org
On 5/28/06, Chris Chabot <chabotc-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:> > Personally i loved the ''json in header'' feature, it allowed me to have > large quantities of html in the responce body, and some params, or other > divs to update, etc in the x-json header ..I assume by large quantities of HTML you mean just 1 large quantity of HTML? With more than one large quantity of HTML, it seems a cleaner design to have them all in JSON and have the entire JSON string in the response body. The JSON vs. response body design seems to encourage putting HTML in the response body which is designed to hold a single HTML fragment. You can still put other HTML fragments in JSON but IMO it seems cleaner to put them all in JSON instead of fragment 1 in the response body and fragments 2-n in JSON. Worked out much better then having to xml parse the responce body into> sepperate entities :-) (like openrico.org does forinstance) >The comparison should be JSON in X-JSON with separate response body vs. JSON in response body. You seem to be comparing JSON in X-JSON with separate response body with XML in response body. JSON uses JavaScript''s eval, not xml parse, so if Rico uses XML it''s probably not a good comparison. John _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 5/28/06, Brice Burgess <bhb-q0spLWLYtkPR7s880joybQ@public.gmane.org> wrote:> > Also note that from my experience, the X-JSON header is CASE SENSITIVE > -- and must all be lowercase.What configuration do you run that causes X-JSON in all caps to fail? I find this interesting because: (a) X-JSON in all caps works fine for me using prototype 1.5.0 rc0 with IE 6, Fx 1.5.0.3 and Fx 1.0.7. (b) It seems that most of the posts on the Internet use all uppercase. For example, [ prototype x-json ] in Google returns pages where X-JSON is in all uppercase. One article says some systems will camelcase X-JSON to X-Json but prototype isn''t affected. I would read that as X-JSON being not case sensitive and most people use all uppercase. (c) I assume all uppercase works for Chris and Dirk. It would be great if you could provide some software and version numbers that cause an all uppercase header to fail. I''m curious to find out what is causing the problem. John _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Also note that from my experience, the X-JSON header is CASE SENSITIVE -- and must all be lowercase. ~ Brice John Wang wrote:> On 5/28/06, *Dirk Eschler* <dirk.eschler-hi6Y0CQ0nG0@public.gmane.org > <mailto:dirk.eschler-hi6Y0CQ0nG0@public.gmane.org>> wrote: > > According to > http://www.sergiopereira.com/articles/prototype.js.html, the > second param is supposed to be the evaluated json object IF the > X-JSON header > is send in the response. I send the header in my script. However, > the json > var is always undefined and i have to evaluate t.responseText myself. > > Am i missing anything important? > > > What version of prototype are you using? I think you need to be > running 1.4.0 or higher. I''m using 1.5.0 rc0 and auto-eval of X-JSON > works. > > It''d be nice if the libraries could agree on how to accept the JSON > response since Dojo and other libraries seem to auto-eval > responseText. This is an issue if you have a generic server-side JSON > library since there doesn''t seem to be a way to identify the library > being used (e.g. user agent). > > John > ------------------------------------------------------------------------ > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
Using prototype 1.5.0_rc0 and the Zend Framework (0.1.3) for the json part i''m able to simple do: $responses = array(''a'' => ''1'', ''b'' => ''2''); header(''X-JSON:''.Zend_Json::encode($responses)); Note i did have to hack Zend_Json first to not quote around the property names, and only quote around the values, but then it all worked fine and dandy -- Chris On Sun, 2006-05-28 at 19:36 -0400, Brice Burgess wrote:> John, > > I was likely "misforming" my headers. However, here''s the PHP code: > > header(''x-json: ''.$encoder->encode($json)); > > $encoder->encode($json); just spits out a valid JSON object. > > If the above was; > > header(''X-JSON: ''.$encoder->encode($json)); > > it wouldn''t work. > > However, I''m under the impression that I''m going about it in the wrong > manner ;) Perhaps I set the header... and then the entire return is > valid JSON? > > ~ Brice > > > > John Wang wrote: > > On 5/28/06, *Brice Burgess* <bhb-q0spLWLYtkPR7s880joybQ@public.gmane.org <mailto:bhb-q0spLWLYtkPR7s880joybQ@public.gmane.org>> > > wrote: > > > > Also note that from my experience, the X-JSON header is CASE SENSITIVE > > -- and must all be lowercase. > > > > > > What configuration do you run that causes X-JSON in all caps to fail? > > > > I find this interesting because: > > > > (a) X-JSON in all caps works fine for me using prototype 1.5.0 rc0 > > with IE 6, Fx 1.5.0.3 <http://1.5.0.3> and Fx 1.0.7. > > > > (b) It seems that most of the posts on the Internet use all uppercase. > > For example, [ prototype x-json ] in Google returns pages where X-JSON > > is in all uppercase. One article says some systems will camelcase > > X-JSON to X-Json but prototype isn''t affected. I would read that as > > X-JSON being not case sensitive and most people use all uppercase. > > > > (c) I assume all uppercase works for Chris and Dirk. > > > > It would be great if you could provide some software and version > > numbers that cause an all uppercase header to fail. I''m curious to > > find out what is causing the problem. > > > > John > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Rails-spinoffs mailing list > > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
John, I was likely "misforming" my headers. However, here''s the PHP code: header(''x-json: ''.$encoder->encode($json)); $encoder->encode($json); just spits out a valid JSON object. If the above was; header(''X-JSON: ''.$encoder->encode($json)); it wouldn''t work. However, I''m under the impression that I''m going about it in the wrong manner ;) Perhaps I set the header... and then the entire return is valid JSON? ~ Brice John Wang wrote:> On 5/28/06, *Brice Burgess* <bhb-q0spLWLYtkPR7s880joybQ@public.gmane.org <mailto:bhb-q0spLWLYtkPR7s880joybQ@public.gmane.org>> > wrote: > > Also note that from my experience, the X-JSON header is CASE SENSITIVE > -- and must all be lowercase. > > > What configuration do you run that causes X-JSON in all caps to fail? > > I find this interesting because: > > (a) X-JSON in all caps works fine for me using prototype 1.5.0 rc0 > with IE 6, Fx 1.5.0.3 <http://1.5.0.3> and Fx 1.0.7. > > (b) It seems that most of the posts on the Internet use all uppercase. > For example, [ prototype x-json ] in Google returns pages where X-JSON > is in all uppercase. One article says some systems will camelcase > X-JSON to X-Json but prototype isn''t affected. I would read that as > X-JSON being not case sensitive and most people use all uppercase. > > (c) I assume all uppercase works for Chris and Dirk. > > It would be great if you could provide some software and version > numbers that cause an all uppercase header to fail. I''m curious to > find out what is causing the problem. > > John > ------------------------------------------------------------------------ > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >
On Sunday 28 May 2006 15:46, Chris Chabot wrote:> Using prototype 1.5.0_rc0 and the Zend Framework (0.1.3) for the json > part i''m able to simple do: > > $responses = array(''a'' => ''1'', ''b'' => ''2''); > header(''X-JSON:''.Zend_Json::encode($responses)); > > Note i did have to hack Zend_Json first to not quote around the property > names, and only quote around the values, but then it all worked fine and > dandyyou actually *should* quote your property names. For instance, if you use the property name ''class'' and it doesn''t have quotes around it, Safari will yell at you. Having quotes around the property names is no less correct than not having quotes around them.. -Jeremy _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On 5/29/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote:> you actually *should* quote your property names. For instance, if you use the > property name ''class'' and it doesn''t have quotes around it, Safari will yell > at you. > > Having quotes around the property names is no less correct than not having > quotes around them..I''m with Jeremy on this one; I quote my property names. I''ve been bitten by the class example above AND I''ve had issues with numeric keys being treated as numbers (ie, 01 should be "01" NOT the octet value 1). Todd