Yesod:在AJAX调用中使用类型安全的URL [英] Yesod: Using typesafe URLs in AJAX calls

查看:101
本文介绍了Yesod:在AJAX调用中使用类型安全的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  / api / hide / thread /#Text /#Int ApiHideThreadR GET 

我想用javascript在客户端请求它:

  function hideThreadCompletely(threadId,board){
$ .getJSON(/ api / hide / thread /+ board +/+ threadId,function (data){
$('#thread - '+ threadId).hide();
});
}

但是我不能使用 @ {ApiHideTHreadR} 因为Yesod需要编译时的参数。如果我希望API URLS看起来像 api / board / 1/1 而不是 api / board?bid = 1安培; TID = 1 ?继续使用/ api / hide / thread /+ board +/+ threadId

解决方案

经过一番搜索,我发现这个讨论,其中建议您将url作为data-url属性添加到某个元素。然后从元素中加载url。
类似于这样:

 < div id =thread-1data-hide-url = @ { ApiHideTHreadR}> 
$ b var url = $(#thread-1)。data(hide-url)


In my Yesod project i have the following route:

/api/hide/thread/#Text/#Int      ApiHideThreadR   GET

I want to request it on the client side with javascript:

function hideThreadCompletely(threadId, board) {
    $.getJSON("/api/hide/thread/"+board+"/"+threadId, function(data) {
        $('#thread-'+threadId).hide();
    });
}

But i can't use @{ApiHideTHreadR} because Yesod requires it's arguments on compile time. What is the proper solution for this, if i want API URLS to look like api/board/1/1 and not api/board?bid=1&tid=1? Keep using manually-defined URL's like "/api/hide/thread/"+board+"/"+threadId?

解决方案

After some searching I found this discussion, where it is suggested that you add the url as a "data-url" attribute to some element. And then load the url from the element. Something like this:

<div id="thread-1" data-hide-url=@{ApiHideTHreadR}>

var url = $("#thread-1").data("hide-url")

这篇关于Yesod:在AJAX调用中使用类型安全的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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