使用Yesod解析JSON时诊断“没有足够的字节”

时间:2013-12-24 01:08:37

标签: json http haskell post yesod

我正在尝试使用以下Yesod代码解析Json POST请求的响应主体。

import qualified Data.Aeson as J
postMypageR = do
  json <- parseJsonBody :: Handler (J.Result J.Value)
  case json of
       J.Error e -> error(show e) 
       J.Success code -> do
         liftIO $ putStrLn $ show code
         defaultLayout $ myWidget

在我的Javascript中,我发送了xmlhttp.send(JSON.stringify({a:3, b:4}));

Firefox Web控制台确认发送到POST的字符串是{"a":3,"b":4}

但是,当我运行代码时,出现以下错误:ParseError {errorContexts = [\"demandInput\"], errorMessage = \"not enough bytes\", errorPosition = 1:1}"

有人知道为什么会这样吗?我的GET和POST都可以在其他页面中使用,但到目前为止,这是我实际查看POST主体的应用程序中唯一的位置。

1 个答案:

答案 0 :(得分:3)

我的猜测是这是一个javascript错误....你在发送之前设置了内容长度吗?

  xmlhttp.setRequestHeader("Content-length", data.length);

其中data=JSON.stringify({a:3, b:4})