如何在此Affjax调用上定义类型签名

时间:2016-04-25 07:59:42

标签: types purescript

我使用Affjax和launchAff来完成这个简单的程序。

import Network.HTTP.Affjax as Affjax
import Control.Monad.Aff (launchAff)

test1 = launchAff $ do Affjax.get "/api"

这给了我以下错误

 58  test1 = launchAff $ do Affjax.get "/api"
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 No type class instance was found for

 Network.HTTP.Affjax.Response.Respondable _0

 The instance head contains unknown type variables. Consider adding a type annotation.

 in value declaration test1

 where _0 is an unknown type

我知道Respondable需要在这里定义,但我真的不知道怎么做。

非常感谢任何帮助。

THX

1 个答案:

答案 0 :(得分:3)

您需要注释test1函数,让编译器知道您希望从该ajax调用中获得什么。

test1 = launchAff do
  Affjax.get "/api" :: Affjax _ Foreign

您可以为返回类型选择these instances中的任何一个,或者如果您希望返回自己的数据类型,请为其编写Respondable实例。

编辑:我编辑了源代码。提供的codesnippet虽然不是很有用,因为你没有对AJAX响应做任何事情。由于AffjaxAff内运行,您可以使用do notation将GET调用的结果绑定到变量,然后从那里开始使用它。