Writing AJAX applications in Haskell

By: on July 13, 2006

How easy is it to write an [AJAX](http://en.wikipedia.org/wiki/AJAX)
application in [Haskell](http://www.haskell.org/)? Very easy
indeed. Here’s a little example of an AJAX server that echoes
[JSON](http://www.json.org/) requests:

module Main where

import Data.Maybe (fromJust)
import JSON
import Network.NewCGI

jsonEcho :: CGI CGIResult
jsonEcho = do jsonString <- getInput “json”
setHeader “Content-type” “application/x-javascript”
output $ (stringify . fromJust . parse . fromJust) jsonString

main = runCGI jsonEcho

To get this to compile you need the following haskell libraries: [Fast
Packed Strings](http://www.cse.unsw.edu.au/~dons/fps.html) (needed by
haskell-cgi), [XHTML
combinators](http://www.cs.chalmers.se/~bringert/darcs/haskell-xhtml/doc/) (needed
by haskell-cgi),
[haskell-cgi](http://www.cs.chalmers.se/~bringert/darcs/haskell-cgi/doc/),
and [JSON.hs](http://www.tom.sfc.keio.ac.jp/~sakai/d/data/200604/JSON.hs)

The resulting executable can be installed on any web server that
supports cgi scripts. For my tests I configured apache with cgi
execution enabled in user directories. You can download the little
example app [here](/downloads/haskell-ajax.tar.gz). The client code employs the
[json](http://www.json.org/js.html) and
[prototype](http://prototype.conio.net/) Javascript libraries.

The JSON.hs file included in this distribution is a slightly modified
version of the one listed above which adds support for Ints.

FacebookTwitterGoogle+

3 Comments

  1. Alberto says:

    Very interesting indeed

  2. Paul Brown says:

    Short, sweet, and timely. I was just looking for a JSON library for Haskell, as a matter of fact…

  3. Robin Bate Boerop says:

    Your article is well focused and densely packed with relevant information. Useful. Thank you.

Post a comment

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>