3

我正在使用 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需要在这里定义,但我真的不知道该怎么做。

任何帮助是极大的赞赏。

谢谢

4

1 回答 1

3

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

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

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

编辑:我编辑了源代码。但是,提供的代码片段并不是很有用,因为您没有对 AJAX 响应做任何事情。由于Affjax在内部进行操作,Aff因此您可以使用 do 表示法将GET调用结果绑定到变量并从那里继续使用它。

于 2016-04-25T08:19:40.960 回答