gpt4 book ai didi

http - Elm Http Api 调用

转载 作者:可可西里 更新时间:2023-11-01 16:36:23 26 4
gpt4 key购买 nike

我是 Elm 的新手。我目前正在尝试自学如何在 Elm 中进行 API 调用。我正在制作的程序非常简单,它发出一个 http 请求来获取一些 Trello 卡片信息并显示它。我的程序可以编译,但是当我单击按钮获取信息时没有任何反应。我不确定我做错了什么请帮忙。谢谢

 module Main exposing (..)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as Decode


main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}


type alias Model =
{ boardName : String
, cardName : String
}


init =
( Model "Default Board" "Default Card"
, Cmd.none
)



-- UPDATE


type Msg
= CardFetch (Result Http.Error String)
| FetchCard


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
FetchCard ->
( model, getCardName )

CardFetch (Ok incomingName) ->
( Model model.cardName incomingName, Cmd.none )

CardFetch (Err _) ->
( model, Cmd.none )



-- HTTP


getCardName =
Http.send CardFetch (Http.get "https://api.trello.com/1/members/..." decodeCard)


decodeCard =
Decode.at [ "data", "card", "name" ] Decode.string



--UPDATE
-- VIEW


view : Model -> Html Msg
view model =
div []
[ div []
[ button [ onClick FetchCard ] [ text "Get Card" ] ]
, div [ class "card" ]
[ h3 [] [ text model.cardName ]
, div [ class "board" ] [ h4 [] [ text model.boardName ] ]
]
]



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none

最佳答案

终于明白了。我不得不用 Decode.index 0 ( Decode.at [ "data", "card", "name"] Decode.string) 将它解码为一个数组

关于http - Elm Http Api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com