gpt4 book ai didi

http - Elm http 请求返回成功请求的 NetworkError

转载 作者:可可西里 更新时间:2023-11-01 15:10:29 25 4
gpt4 key购买 nike

我有相当多的使用 React 构建网络应用程序的经验,但我想学习 Elm。几天来,我一直在努力解决 HTTP 请求问题。

我在 localhost:8080 运行一个 Elm 应用程序,在 localhost:8081 运行我的支持 API。每当我发出 HTTP 请求(我尝试了 GET 和 POST 请求)时,我都会收到 NetworkError。我一直在研究 Elm JSON 解码器,认为这可能是我的问题所在,但我尝试从我的服务器发送简单的字符串并在我的 Elm 应用程序中使用 Decode.string 解码器,但我仍然遇到 NetworkError。

这是我的代码目前的样子:

命令.elm

module Commands exposing (..)

import Models exposing (..)
import Msg exposing (..)
import Http
import Json.Decode as Decode
import Json.Encode as Encode


createTempUser : Model -> Cmd Msg
createTempUser model =
let
tempUserBody =
[ ( "firstname", Encode.string model.firstname )
, ( "lastname", Encode.string model.lastname )
, ( "phone", Encode.string model.phone )
]
|> Encode.object
|> Http.jsonBody

url =
myAPIUrl ++ "/endpoint"

contentType = Http.header "Content-type" "text/plain"

post =
Http.request
{ method = "POST"
, headers = [contentType]
, url = url
, body = tempUserBody
, expect = Http.expectJson decodeApiResponse
, timeout = Nothing
, withCredentials = False
}

in
Http.send Msg.TempUserCreated post


decodeApiResponse : Decode.Decoder ApiResponse
decodeApiResponse =
Decode.map4 ApiResponse
(Decode.at ["status"] Decode.int)
(Decode.at ["message"] Decode.string)
(Decode.at ["created"] Decode.int)
(Decode.at ["error"] Decode.string)


myAPIUrl : String
myAPIUrl = "http://localhost:8081"

模型.elm

module Models exposing (..)


type alias ApiResponse =
{ status: Int
, message: String
, created: Int
, error: String
}

消息.elm

module Msg exposing (..)

import Navigation exposing (Location)
import Models exposing (..)
import Http


type Msg
= ChangeLocation String
| OnLocationChange Location
| CreateTemporaryUser
| TempUserCreated ( Result Http.Error ApiResponse )

更新.elm

module Update exposing (..)

import Msg exposing (Msg)
import Models exposing (..)
import Routing exposing (..)
import Commands exposing (..)
import Navigation

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Msg.ChangeLocation path ->
( { model | changes = model.changes + 1 }, Navigation.newUrl path )

Msg.OnLocationChange location ->
( { model | error = "", route = parseLocation(location) }, Cmd.none )

Msg.CreateTemporaryUser ->
( model, createTempUser model )

Msg.TempUserCreated (Ok res) ->
update (Msg.ChangeLocation signupCodePath) { model | httpResponse = toString(res) }

Msg.TempUserCreated (Err err) ->
( { model | error = toString(err) }, Cmd.none )

Chrome 的网络开发工具将响应显示为这样

{"status":200,"message":"Successfully inserted temporary 
user","created":1518739596447,"error":""}

我认为这可能是所有相关代码,但如果您需要查看更多代码,我会进行更新,包括请求的代码。我承认我对 Elm Json.Decode 库没有完全的了解,但我的印象是,如果这是问题所在,我会得到一个包含额外上下文的 UnexpectedPayload 错误。

最佳答案

@Sidney 和@SimonH,

感谢您为我调查此事,我感到很难过,因为这毕竟不是 Elm 的问题。

该解决方案最终使用服务器上的 CORS 中间件来添加“Access-Control-Allow-Origin” header 。服务器仍然以 200 状态响应,甚至包括整个成功的响应正文,但这导致 Elm 的 Http 结果失败。

再次感谢您的宝贵时间!

关于http - Elm http 请求返回成功请求的 NetworkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48818455/

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