Writing AJAX applications in Haskell

July 13th, 2006 matthias

How easy is it to write an AJAX application in Haskell? Very easy indeed. Here’s a little example of an AJAX server that echoes JSON 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 (needed by haskell-cgi), XHTML combinators (needed by haskell-cgi), haskell-cgi, and 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. The client code employs the json and prototype 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.

Entry Filed under: Technology, Programming, Our Software

3 Comments Add your own

  • 1. Alberto  |  August 12th, 2006 at 11:23 pm

    Very interesting indeed

  • 2. Paul Brown  |  September 29th, 2006 at 12:30 am

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

  • 3. Robin Bate Boerop  |  October 12th, 2007 at 3:21 pm

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

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Calendar

July 2006
M T W T F S S
« Jun   Aug »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Most Recent Posts