--- title: "Hello Yesod + Ajax" date: "2016-09-02" output: html_document --- ```{r setup, include=FALSE} knitr::opts_knit$set(root.dir = "./assets/haskell/") ## a small modification of the haskell engine provided by knitr: ## (to get rid of the option ':set +m') knitr::knit_engines$set(ghc = function (options) { engine = options$engine f = basename(tempfile(engine, ".", ".txt")) writeLines(options$code, f) on.exit(unlink(f)) code = paste(f, options$engine.opts) cmd = options$engine.path out = if (options$eval) { message("running: ", cmd, " ", code) tryCatch(system2(cmd, code, stdout = TRUE, stderr = FALSE, env = options$engine.env), error = function(e) { if (!options$error) stop(e) paste("Error in running command", cmd) }) } else "" if (!options$error && !is.null(attr(out, "status"))) stop(paste(out, collapse = "\n")) knitr::engine_output(options, options$code, out) } ) ## chunk options knitr::opts_chunk$set(engine = 'ghc', engine.path = 'ghcscriptrender', engine.opts = '--fragment --module', echo = FALSE, results = 'asis') ``` Nous donnons un exemple des requêtes POST, PUT et GET dans une application Yesod. ## POST ![](assets/img/yesod_post.png) La requête POST permet d'obtenir un objet défini dans le code Haskell (ici le tableau `[1,2,3]` retourné par la fonction `postJsonR`). ```{r post} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE ViewPatterns #-} import Yesod import Yesod.Form.Jquery (YesodJquery (urlJqueryJs)) data HelloWorld = HelloWorld mkYesod "HelloWorld" [parseRoutes| / HomeR GET /json JsonR POST |] instance Yesod HelloWorld instance YesodJquery HelloWorld getHomeR :: Handler Html getHomeR = defaultLayout $ do setTitle "Hello POST" getYesod >>= addScriptEither . urlJqueryJs [whamlet|