http只能获取httpbin.org
Http can only get httpbin.org
当 运行 elm-reactor
中的以下程序(或为此使用 elm-make
)时,我发现 只有 网页它实际上 GET
s 是 httpbin. Otherwise, I see a Http.NetworkError, even on reliable sites such as "http://google.com" or "http://whosebug.com”。我完全不明白为什么会这样,有人能指出我的错误吗?
module Main (..) where
import Http
import Html exposing (..)
import Effects
import Html.Events as Events
import StartApp
import Task
-- MODEL
type alias Model =
{ output : String }
type Action
= Response String
| Request String
| HTTPError Http.Error
-- UPDATE
update : Action -> Model -> ( Model, Effects.Effects Action )
update act mod =
case act of
Response str ->
( { mod | output = str }, Effects.none )
Request srv ->
let
effects =
srv
|> Http.getString
|> Task.map Response
|> flip Task.onError (Task.succeed << HTTPError)
|> Effects.task
in
( { mod | output = "GET: " ++ srv }, effects )
HTTPError err ->
( { mod
| output =
"Error: "
++ case err of
Http.Timeout ->
"Timeout"
Http.UnexpectedPayload str ->
"Unexpected payload: " ++ str
Http.BadResponse code str ->
"Bad response: " ++ toString code ++ ": " ++ str
Http.NetworkError ->
"Network error"
}
, Effects.none
)
-- VIEW
view : Signal.Address Action -> Model -> Html.Html
view address mod =
div
[]
[ div
[]
[ input
[ Events.on "input" Events.targetValue (Request >> Signal.message address) ]
[]
]
, div [] [ text mod.output ]
]
-- MAIN
app : StartApp.App Model
app =
StartApp.start
{ init = ( { output = "No requests made" }, Effects.none )
, update = update
, view = view
, inputs = []
}
main =
app.html
port tasks : Signal (Task.Task Effects.Never ())
port tasks =
app.tasks
您很可能 运行 受到 cross-origin 浏览器限制。当您发出失败的请求时,请查看您的浏览器 javascript 控制台。 Chrome 记录如下错误:
XMLHttpRequest cannot load https://www.whosebug.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://elm-lang.org' is therefore not allowed access.
您的工作 httpbin 示例 link 包括 HTTP header Access-Control-Allow-Origin: *
,这是限制最少的设置。
您可以通过查找有关 CORS(代表跨源 Http 请求)的信息,详细了解这些类型的问题以及解决这些问题的方法。
当 运行 elm-reactor
中的以下程序(或为此使用 elm-make
)时,我发现 只有 网页它实际上 GET
s 是 httpbin. Otherwise, I see a Http.NetworkError, even on reliable sites such as "http://google.com" or "http://whosebug.com”。我完全不明白为什么会这样,有人能指出我的错误吗?
module Main (..) where
import Http
import Html exposing (..)
import Effects
import Html.Events as Events
import StartApp
import Task
-- MODEL
type alias Model =
{ output : String }
type Action
= Response String
| Request String
| HTTPError Http.Error
-- UPDATE
update : Action -> Model -> ( Model, Effects.Effects Action )
update act mod =
case act of
Response str ->
( { mod | output = str }, Effects.none )
Request srv ->
let
effects =
srv
|> Http.getString
|> Task.map Response
|> flip Task.onError (Task.succeed << HTTPError)
|> Effects.task
in
( { mod | output = "GET: " ++ srv }, effects )
HTTPError err ->
( { mod
| output =
"Error: "
++ case err of
Http.Timeout ->
"Timeout"
Http.UnexpectedPayload str ->
"Unexpected payload: " ++ str
Http.BadResponse code str ->
"Bad response: " ++ toString code ++ ": " ++ str
Http.NetworkError ->
"Network error"
}
, Effects.none
)
-- VIEW
view : Signal.Address Action -> Model -> Html.Html
view address mod =
div
[]
[ div
[]
[ input
[ Events.on "input" Events.targetValue (Request >> Signal.message address) ]
[]
]
, div [] [ text mod.output ]
]
-- MAIN
app : StartApp.App Model
app =
StartApp.start
{ init = ( { output = "No requests made" }, Effects.none )
, update = update
, view = view
, inputs = []
}
main =
app.html
port tasks : Signal (Task.Task Effects.Never ())
port tasks =
app.tasks
您很可能 运行 受到 cross-origin 浏览器限制。当您发出失败的请求时,请查看您的浏览器 javascript 控制台。 Chrome 记录如下错误:
XMLHttpRequest cannot load https://www.whosebug.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://elm-lang.org' is therefore not allowed access.
您的工作 httpbin 示例 link 包括 HTTP header Access-Control-Allow-Origin: *
,这是限制最少的设置。
您可以通过查找有关 CORS(代表跨源 Http 请求)的信息,详细了解这些类型的问题以及解决这些问题的方法。