获取请求时出现 Elm NetworkError,但控制台表示没问题
Elm NetworkError on get request, but console says it's fine
我正在用 Elm 构建我的第一个 Web 应用程序,我遇到了一个问题,当我向本地服务器发出获取请求时,Elm 说它是 'NetworkError',即使浏览器控制台说它是成功了。
我做了一个最小的例子如下:
服务器由 Haskell 和 Scotty:
组成
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Scotty
import Data.Text.Lazy (pack)
main :: IO ()
main = scotty 3000 $ get "/" $ text $ pack "a"
当您向 localhost:3000 发出获取请求时,它所做的只是用字母 'a' 进行响应。如果有一个 Elm 应用程序只显示响应,如果有一个则显示错误,还有一个按钮来执行获取请求:
import Http exposing (getString, send, Error)
import Html exposing (Html, text, br, button, body, program)
import Html.Events exposing (onClick)
type Msg
= DataRequest (Result Error String)
| Refresh
type alias Model =
{ response : String
, err : String
}
main : Program Never Model Msg
main = program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions _ = Sub.none
init : (Model, Cmd Msg)
init = ({ response = "", err = ""} , Cmd.none)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
DataRequest (Err err) -> ({ model | err = toString err}, Cmd.none)
DataRequest (Ok response) -> ({model | response = response}, Cmd.none)
Refresh -> (model, send DataRequest (getString "http://localhost:3000"))
view : Model -> Html Msg
view {response, err} =
body []
[ text <| "Response: " ++ response
, br [] []
, text <| "Error: " ++ err
, br [] []
, button [ onClick Refresh ] [ text "Send request" ]
]
这是我发出获取请求时浏览器控制台的屏幕截图。我对这些东西了解不多,但就我所见,它看起来不错。
我想要的是 'Response' 后面显示字母 'a',而 'Error' 为空白。相反 'Error' 是 'NetworkError' 而 'Response' 是空白的。
我已将这个最小示例放在 Git 存储库中,以防有人愿意尝试并告诉我哪里出了问题:
https://github.com/8n8/elmnetworkerror
(我知道 上有一个非常相似的问题,但还没有答案,我认为它会从一个完整的最小示例中受益。)
我不能说你的错误是什么,但由于 CORS,我得到了一个错误:
Main.elm:1 Failed to load http://localhost:3000/:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8000' is therefore not allowed access.
所以我已经调整了您的 Main.{hs,elm}
以从发生访问的同一服务器提供文件:
Main.elm
Refresh -> (model, send DataRequest (getString "/data"))
Main.hs
main = scotty 3000 $ do
get "/" $ file "../index.html"
get "/data" $ text $ pack "a"
而不是使用 elm-reactor
我 运行 elm-make src/Main.elm --output=index.html
我正在用 Elm 构建我的第一个 Web 应用程序,我遇到了一个问题,当我向本地服务器发出获取请求时,Elm 说它是 'NetworkError',即使浏览器控制台说它是成功了。
我做了一个最小的例子如下:
服务器由 Haskell 和 Scotty:
组成{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Scotty
import Data.Text.Lazy (pack)
main :: IO ()
main = scotty 3000 $ get "/" $ text $ pack "a"
当您向 localhost:3000 发出获取请求时,它所做的只是用字母 'a' 进行响应。如果有一个 Elm 应用程序只显示响应,如果有一个则显示错误,还有一个按钮来执行获取请求:
import Http exposing (getString, send, Error)
import Html exposing (Html, text, br, button, body, program)
import Html.Events exposing (onClick)
type Msg
= DataRequest (Result Error String)
| Refresh
type alias Model =
{ response : String
, err : String
}
main : Program Never Model Msg
main = program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions _ = Sub.none
init : (Model, Cmd Msg)
init = ({ response = "", err = ""} , Cmd.none)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
DataRequest (Err err) -> ({ model | err = toString err}, Cmd.none)
DataRequest (Ok response) -> ({model | response = response}, Cmd.none)
Refresh -> (model, send DataRequest (getString "http://localhost:3000"))
view : Model -> Html Msg
view {response, err} =
body []
[ text <| "Response: " ++ response
, br [] []
, text <| "Error: " ++ err
, br [] []
, button [ onClick Refresh ] [ text "Send request" ]
]
这是我发出获取请求时浏览器控制台的屏幕截图。我对这些东西了解不多,但就我所见,它看起来不错。
我想要的是 'Response' 后面显示字母 'a',而 'Error' 为空白。相反 'Error' 是 'NetworkError' 而 'Response' 是空白的。
我已将这个最小示例放在 Git 存储库中,以防有人愿意尝试并告诉我哪里出了问题:
https://github.com/8n8/elmnetworkerror
(我知道
我不能说你的错误是什么,但由于 CORS,我得到了一个错误:
Main.elm:1 Failed to load http://localhost:3000/:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:8000' is therefore not allowed access.
所以我已经调整了您的 Main.{hs,elm}
以从发生访问的同一服务器提供文件:
Main.elm
Refresh -> (model, send DataRequest (getString "/data"))
Main.hs
main = scotty 3000 $ do
get "/" $ file "../index.html"
get "/data" $ text $ pack "a"
而不是使用 elm-reactor
我 运行 elm-make src/Main.elm --output=index.html