XhrRequest with reflex/reflex-dom

som*_*unt 7 haskell ghcjs reflex

我想执行一个基本的Ajax请求,就是这样.

reflex用于前端和Scotty后端.Firefox Web控制台告诉我请求是成功的,我在那里看到了预期的结果.但网站切换Just "default"Nothing而不是Just "success!".

这是一个完整的最小例子:

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
Run Code Online (Sandbox Code Playgroud)

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!"
Run Code Online (Sandbox Code Playgroud)

由于调试工具告诉我请求是成功的,我怀疑错误在某处附近,decodeXhrResponse但我有点失去了我应该继续调试,因为它只是编译成(不可读)Javascript.

我使用GitHub的try-reflex Nix脚本来设置所有内容并ghcjs hello.hs在Nix环境中编译.

编辑:添加输出curl:

$ curl -G http://localhost:3000/mystring
success!% 
Run Code Online (Sandbox Code Playgroud)

som*_*unt 6

从帮助#reflex-frpfreenode上我发现了一个解决方案:更换decodeXhrResponse_xhrResponse_body使用Text默认字符串的工作:

buttonDyn <- holdDyn (Just $ T.pack "default") $ fmap _xhrResponse_body asyncEvent
Run Code Online (Sandbox Code Playgroud)

decodeXhrResponse 期待某种JSON,虽然我曾尝试通过Scotty服务JSON,但它仍然没有用.