Elm NetworkError upon receipt of the request, but the console says that this is normal

I am building my first web application in Elm and I have this problem: when I receive a request to the local server, Elm says that it is a "NetworkError", although the browser console says that it worked.

I made a minimal example as follows:

Server created using Haskell and Scotty :

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Web.Scotty
import Data.Text.Lazy (pack)

main :: IO ()
main = scotty 3000 $ get "/" $ text $ pack "a"

Everything that he does is answered with the letter “a” when you receive a request to localhost: 3000. The Elm application simply displays the answer, if there is one, and an error if there is one and a button for a request to receive with:

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" ]
    ]

Here is a screenshot of the browser console when I receive the request. I'm not very good at it, but as I understand it, it looks great. Screenshot of the browser console

, "" "" , "" - . "" "NetworkError" "Response" .

Git, - , , :

https://github.com/8n8/elmnetworkerror

( , Elm http request NetworkError , , , .)

+4
1

, , - 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

+6

Source: https://habr.com/ru/post/1693844/


All Articles