weixin_33711647 2013-09-17 22:24 采纳率: 0%
浏览 30

Yesod和REST Ajax

I'm using Yesod to build an Ajax app (using jQuery, though I don't think that matters too much for my question). Basically, what I'd like is for the server to send different representations of the same data depending on whether or not the XMLHttpRequest header got sent. (The point of all of this is to use a javascript library like history.js

In particular, I'd like to have a route like:

/picture/#PictureId GET

Which, when accessed without the XHR header, gets handled by going to the default layout -- or better yet, by a widget which will ultimately get wrapped by the default layout, and when accessed by an XHR request, just sends an HTML representation of the widget.

How should I approach this? I guess I can make a custom defaultLayout-like function to wrap Widgets in logic. Is that sensible, or is there a better approach?

Edit: I decided to override the defaultLayout method in the Yesod class to:

defaultLayout widget = do
    req <- waiRequest
    let reqwith = lookup "X-Requested-With" $ requestHeaders req
    when (maybe False (== "XMLHttpRequest") reqwith) $ do
      (PageContent _ _ w) <- widgetToPageContent widget

      giveUrlRenderer $ [hamlet| ^{w} |]
    ...

But now I'm getting a type error I don't quite understand

Couldn't match type `blaze-markup-0.5.1.5:Text.Blaze.Internal.MarkupM ()'
              with `()'
Expected type: HandlerT App IO ()
  Actual type: HandlerT
                 App IO (blaze-markup-0.5.1.5:Text.Blaze.Internal.MarkupM ())
  • 写回答

1条回答 默认 最新

  • weixin_33698823 2013-09-18 04:44
    关注

    You should take a look at this chapter: http://www.yesodweb.com/book/restful-content.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况