XhrRequest with reflex

I want to execute the main Ajax request, that's all.

I use reflex for the interface and Scotty for the backend. The Firefox web console reports that the request was successful, and I see the expected result. But the website is switching from Just "default" to Nothing instead of Just "success!" .

Here is a complete minimal example:

 import Reflex (holdDyn) import Reflex.Dom (button, el, mainWidget, display) import Reflex.Dom.Xhr (performRequestAsync, xhrRequest, decodeXhrResponse) import Reflex.Class (tag, constant) import Data.Default (def) main :: IO () main = do mainWidget $ el "div" $ do buttonEvent <- button "click me" let defaultReq = xhrRequest "GET" "mystring" def --served by Scotty asyncEvent <- performRequestAsync (tag (constant defaultReq) buttonEvent) buttonDyn <- holdDyn (Just "default") $ fmap decodeXhrResponse asyncEvent display buttonDyn 

and Scotty :

 {-# LANGUAGE OverloadedStrings #-} import Web.Scotty import Network.Wai.Middleware.Static main = scotty 3000 $ do middleware $ staticPolicy (noDots >-> addBase "/mnt/b/haskell/try-reflex/hello.jsexe") get "/" $ do file "/mnt/b/haskell/try-reflex/hello.jsexe/index.html" get "/mystring" $ html "success!" 

Since the debugging tools tell me that the request was successful, I suspect that the error is somewhere near decodeXhrResponse , but I lost a bit how I should continue debugging, as it just compiles into (unreadable) Javascript.

I used the try-reflex Nix script from GitHub to configure everything and compile with ghcjs hello.hs in a Nix environment.

Edit: adding curl output:

 $ curl -G http://localhost:3000/mystring success!% 
+7
haskell ghcjs reflex
source share
1 answer

Using #reflex-frp on freenode, I found a solution: replacing decodeXhrResponse with _xhrResponse_body and using Text for the default string:

 buttonDyn <- holdDyn (Just $ T.pack "default") $ fmap _xhrResponse_body asyncEvent 

decodeXhrResponse expecting some kind of JSON, and although I tried to serve JSON through Scotty, at some point it still wasn't working.

+6
source share

All Articles