Re: [cgiapp] Ajax and JSON recipe

2009-04-11 Thread Bruce McKenzie
That's how I do it, too. JSON.pm does the outgoing conversion:

sub get_json {
my $perl_data_structure = shift;
use JSON; #  imports objToJson
return objToJson( $perl_data_structure );
}

jQuery has lots of tools for handling incoming JSON and for sending form
data to CGI::Application webapps (where it will be handled by CGI.pm).

Cheers,

Bruce


--
Bruce McKenzie
Business Information Graphics, Inc.
New York

http://www.2MinuteExplainer.com
212-477-4288



On Thu, Apr 9, 2009 at 11:20 AM, P Kishor punk.k...@gmail.com wrote:

 On Thu, Apr 9, 2009 at 9:53 AM, Lyle Brooks bro...@deseret.com wrote:
  I have a need to incorporate Ajax calls with JSON data into
  our application.  I was looking for something in terms of a piece
  of starter code or a recipe.  I didn't see anything on the website
  related to it.
 
  CGI Application seems like such a robust framework, that this should be
  a natural fit.
 
  Does anyone have a short starter example of code (both server and client)
  that they would be willing to post/share that would show Ajax/JSON
  usage with CGI::Application?

 Short advice... don't. That is, don't mix CGI::App in the backend with
 Ajax in the front-end. Keep separate things separate.

 Create your JSON stream (I query the db, and convert the result set
 into JSON using JSON.pm) and send it back to the client via
 HTML::Template (I have a simple, one variable template with TMPL VAR
 JSON_DB_QUERY or something like that).

 Use your favorite way of manipulating the data on the client (I use
 jQuery to initiate the Ajax call and to deal with the results).


 
  Thanks much.
 
  Lyle Brooks
 
  #  CGI::Application community mailing list  
  ####
  ##  To unsubscribe, or change your message delivery options,  ##
  ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
  ####
  ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
  ##  Wiki:  http://cgiapp.erlbaum.net/ ##
  ####
  
 
 



 --
 Puneet Kishor http://www.punkish.org/
 Nelson Institute for Environmental Studies http://www.nelson.wisc.edu/
 Carbon Model http://carbonmodel.org/
 Open Source Geospatial Foundation http://www.osgeo.org/

 #  CGI::Application community mailing list  
 ####
 ##  To unsubscribe, or change your message delivery options,  ##
 ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
 ####
 ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
 ##  Wiki:  http://cgiapp.erlbaum.net/ ##
 ####
 



#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




Re: [cgiapp] Ajax and JSON recipe

2009-04-11 Thread Michael Peters

Lyle Brooks wrote:


Does anyone have a short starter example of code (both server and client)
that they would be willing to post/share that would show Ajax/JSON 
usage with CGI::Application?


The client is just Javascript and it's best to pick a framework to handle all 
the messy browser details for you. I use Prototype, but I like JQuery as well. 
The details of how to use them depend on the framework, so I won't put those 
examples in here. And then for the backend, using CGI::Application::Plugin::JSON 
makes it really easy.


Also, there are several ways to JSON in your application. You can just return 
full JSON structures as the body of their reply.


  return $self-json_body({ foo = 1, bar = 2});

Sometimes it's nice to return HTML in the body but with some extra information 
in your HTTP headers as JSON:


  $self-json_header({ valid = 1 });
  return $html_tmpl-output;

And then there is JSONP (http://en.wikipedia.org/wiki/JSON#JSONP) which is 
basically returning Javascript that executes over some JSON. Really nice and 
simple for mashups, etc.


--
Michael Peters
Plus Three, LP


#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




Re: [cgiapp] Ajax and JSON recipe

2009-04-11 Thread Ron Savage
Hi Folks

On Thu, 2009-04-09 at 10:20 -0500, P Kishor wrote:
 On Thu, Apr 9, 2009 at 9:53 AM, Lyle Brooks bro...@deseret.com wrote:
  I have a need to incorporate Ajax calls with JSON data into
  our application.  I was looking for something in terms of a piece
  of starter code or a recipe.  I didn't see anything on the website
  related to it.
 
  CGI Application seems like such a robust framework, that this should be
  a natural fit.
 
  Does anyone have a short starter example of code (both server and client)
  that they would be willing to post/share that would show Ajax/JSON
  usage with CGI::Application?
 
 Short advice... don't. That is, don't mix CGI::App in the backend with
 Ajax in the front-end. Keep separate things separate.

Err,  What? I /think/ you contradict yourself below...

 Create your JSON stream (I query the db, and convert the result set
 into JSON using JSON.pm) and send it back to the client via
 HTML::Template (I have a simple, one variable template with TMPL VAR
 JSON_DB_QUERY or something like that).

Aren't you mixing just like you said you shouldn't?

Actually, I do exactly as you do in querying the db, etc.

 Use your favorite way of manipulating the data on the client (I use
 jQuery to initiate the Ajax call and to deal with the results).

I use YUI http://developer.yahoo.com/yui/ which I find superb.

One of our list members, Peter Karman, put me on to it.

I don't have a simple, stand-alone demo, although I do have a large
web-bases address book program I built.

And, I do have a full weekend marking papers, but I long been thinking
of expanding my CGI::Application::Demo set to include Javascript via
YUI, and in particular Ajax.

If you can just hold your breathe for a couple of days, I'll see what I
can do.

In the meantime, install YUI (it's just a matter of unzipping it), and
start reading Connection Manager, which is their name for Ajax.

Try the demos, etc, etc.

Also, tell us a bit about your test env. I run Apache myself, but do use
mod_perl except on rare occasions. I do use FCGI::ProcManager.

-- 
Ron Savage
r...@savage.net.au
http://savage.net.au/index.html



#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




Re: [cgiapp] Ajax and JSON recipe

2009-04-11 Thread Ron Savage
Hi Lyle

I've written a new module, CGI::Application::Demo::Ajax, which is a
small search engine to retrieve records via Ajax and Json.

I'll document it next and release it to CPAN.
-- 
Ron Savage
r...@savage.net.au
http://savage.net.au/index.html



#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




Re: [cgiapp] Ajax and JSON recipe

2009-04-11 Thread fREW Schmidt

 I have a need to incorporate Ajax calls with JSON data into
 our application.  I was looking for something in terms of a piece
 of starter code or a recipe.  I didn't see anything on the website
 related to it.

 CGI Application seems like such a robust framework, that this should be
 a natural fit.

 Does anyone have a short starter example of code (both server and client)
 that they would be willing to post/share that would show Ajax/JSON
 usage with CGI::Application?

 Thanks much.


The project we are working on right now is entirely javascript on the
frontend; with a single 1-2K html file to load it all up.  One of the most
interesting things we did to make everything nice on the js side was to make
a URL generation function that you pass the controller and the runmode, and
it generates the url for that.  The great thing about that is that it
allowed us to explore using mod_perl, CGI::Appliation::Dispatch, and all
kinds of other stuff by just changing the generation function.

As for how to do it on the server side; I agree with what everyone else said
:-)  CGI::Application::Plugin::JSON has treated me well.



-- 
fREW Schmidt
http://blog.afoolishmanifesto.com

#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




[cgiapp] Ajax and JSON recipe

2009-04-09 Thread Lyle Brooks
I have a need to incorporate Ajax calls with JSON data into
our application.  I was looking for something in terms of a piece
of starter code or a recipe.  I didn't see anything on the website
related to it.

CGI Application seems like such a robust framework, that this should be
a natural fit.

Does anyone have a short starter example of code (both server and client)
that they would be willing to post/share that would show Ajax/JSON 
usage with CGI::Application?

Thanks much.

Lyle Brooks

#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####




Re: [cgiapp] Ajax and JSON recipe

2009-04-09 Thread P Kishor
On Thu, Apr 9, 2009 at 9:53 AM, Lyle Brooks bro...@deseret.com wrote:
 I have a need to incorporate Ajax calls with JSON data into
 our application.  I was looking for something in terms of a piece
 of starter code or a recipe.  I didn't see anything on the website
 related to it.

 CGI Application seems like such a robust framework, that this should be
 a natural fit.

 Does anyone have a short starter example of code (both server and client)
 that they would be willing to post/share that would show Ajax/JSON
 usage with CGI::Application?

Short advice... don't. That is, don't mix CGI::App in the backend with
Ajax in the front-end. Keep separate things separate.

Create your JSON stream (I query the db, and convert the result set
into JSON using JSON.pm) and send it back to the client via
HTML::Template (I have a simple, one variable template with TMPL VAR
JSON_DB_QUERY or something like that).

Use your favorite way of manipulating the data on the client (I use
jQuery to initiate the Ajax call and to deal with the results).



 Thanks much.

 Lyle Brooks

 #  CGI::Application community mailing list  
 ##                                                            ##
 ##  To unsubscribe, or change your message delivery options,  ##
 ##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
 ##                                                            ##
 ##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
 ##  Wiki:          http://cgiapp.erlbaum.net/                 ##
 ##                                                            ##
 





-- 
Puneet Kishor http://www.punkish.org/
Nelson Institute for Environmental Studies http://www.nelson.wisc.edu/
Carbon Model http://carbonmodel.org/
Open Source Geospatial Foundation http://www.osgeo.org/

#  CGI::Application community mailing list  
####
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp##
####
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:  http://cgiapp.erlbaum.net/ ##
####