从 yesod 处理程序返回 JSON [英] Return JSON from yesod handler

查看:20
本文介绍了从 yesod 处理程序返回 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Yesod 的处理程序中编写一个最简单的 JSON 响应,但有一些非常愚蠢的错误(显然).我的处理程序代码是这样的:

I'm trying to write a simplest JSON response from Yesod's handler, but have some really stupid error (apparently). My handler code is this:

-- 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...

推荐答案

我也遇到了这个问题:你只需要改变你的类型签名,它就会起作用:

I got caught by this one too: you just have to change your type signature and it will work:

getEchoR :: String -> Handler Value

我的理解是整个 Rep 系统在 Yesod 1.2 中已弃用,因此处理程序现在返回 Html 和 Value 而不是 RepHtml 和 RepJson.

My understanding is that the whole Rep system is deprecated in Yesod 1.2, so Handler's now return Html and Value rather than RepHtml and RepJson.

希望这会有所帮助!

这篇关于从 yesod 处理程序返回 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆