我正在尝试从 Yesod 的处理程序编写一个最简单的 JSON 响应,但有一些非常愚蠢的错误(显然)。我的处理程序代码是这样的:
-- HelloYesod/Handler/Echo.hs
module Handler.Echo where
import Data.Aeson (object, (.=))
import qualified Data.Aeson as J
import Data.Text (pack)
import Import
import Yesod.Core.Json (returnJson)
getEchoR :: String -> Handler RepJson
getEchoR theText = do
let json = object $ ["data" .= "val"]
return json
错误是这样的:
Handler/Echo.hs:12:10:
Couldn't match expected type `RepJson' with actual type `Value'
In the first argument of `return', namely `json'
In a stmt of a 'do' block: return json
In the expression:
do { let json = object $ ...;
return json }
Build failure, pausing...
最佳答案
我也被这个问题困扰了:你只需要更改你的类型签名,它就会起作用:
getEchoR :: String -> Handler Value
我的理解是,整个 Rep 系统在 Yesod 1.2 中已被弃用,因此 Handler 现在返回 Html 和 Value,而不是 RepHtml 和 RepJson。
希望这有帮助!
关于json - 从 yesod 处理程序返回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282501/