Haskell Scotty和Elm Http NetworkError

Har*_*her 5 haskell httprequest web-development-server elm scotty

我想通过haskell用于后端和elm用于前端来研究Web开发.所以我写了这两个简单的"hello world"代码片段

榆树:

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

main : Program Never Model Msg
main = Html.program
  { view = view
  , update = update
  , init = ("default", Cmd.none)
  , subscriptions = \_ -> Sub.none }

type alias Model = String

type Msg = Get | Response (Result Http.Error String)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model = case msg of
  Get -> (model, get)
  Response (Ok s) -> (s, Cmd.none)
  Response (Err e) -> (toString e, Cmd.none)


view : Model -> Html Msg
view model = div []
  [button [onClick (Get)] [text "click me"],
   text model]


get : Cmd Msg
get =  let url = "http://localhost:3000/get"
       in Http.send Response (Http.get url Decode.string)
Run Code Online (Sandbox Code Playgroud)

哈斯克尔/斯科蒂:

import Web.Scotty

main = scotty 3000 $ get "/get" $ json ("hello world" :: String)
Run Code Online (Sandbox Code Playgroud)

两者都完美地工作 - 这意味着榆树代码可以从像httpbin这样的服务器获取数据,而scotty服务器处理我用浏览器或wget/curl等工具发送的请求,但当我尝试将两者一起使用时,http.send call in elm返回网络错误

我怀疑这可能是一个问题,两台服务器都托管在同一台计算机上(不知道为什么,但我想消除这种可能性)所以我在另一台计算机上托管客户端站点,我知道它与计算机有一个有效连接托管spock后端(与wget等一起工作)但它仍然无效.

我错过了一些明显的东西,或者问题是什么?thx提前

Cha*_*ert 9

听起来您的问题是由于跨源请求共享(CORS)限制.您可以使用wai-cors来设置CORS策略.

例:

import Web.Scotty
import Network.Wai.Middleware.Cors

main = scotty 3000 $ do
    middleware simpleCors
    get "/get" $ json ("hello world" :: String)
Run Code Online (Sandbox Code Playgroud)