我想研究一下网络开发的后端haskell和前端的elm。所以我写了两个简单的"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)哈斯克尔/斯科蒂:
import Web.Scotty
main = scotty 3000 $ get "/get" $ json ("hello world" :: String)这两种方法都能很好地独立工作--这意味着elm代码可以从httpbin等服务器获得数据,scotty服务器可以处理我用浏览器发送的请求或wget/curl等工具,但是当我试图将两者结合使用时,elm中的http.send调用将返回一个网络错误。
我怀疑这可能是一个问题,两台服务器都托管在同一台计算机上(不知道原因,但我希望消除这种可能性),因此我在另一台计算机上托管了客户端站点,我知道它与承载spock后端的计算机有工作连接(与wget等一起工作),但它仍然无法工作。
我是不是遗漏了一些显而易见的东西,还是有什么问题?thx预先
发布于 2018-06-05 18:09:33
听起来你的问题是由于跨源请求共享(CORS)的限制。您可以使用围哥来设置CORS策略。
示例:
import Web.Scotty
import Network.Wai.Middleware.Cors
main = scotty 3000 $ do
middleware simpleCors
get "/get" $ json ("hello world" :: String)https://stackoverflow.com/questions/50706306
复制相似问题