我一直在努力在我的Elm客戶端和長頸鹿服務器之間啓用跨域請求。儘管在Elm和長頸鹿中啓用了CORS,但Http Post的問題
榆樹(客戶端):
tryPostRegistration : String -> Http.Body -> Decoder JsonProfile -> Http.Request JsonProfile
tryPostRegistration url body decoder =
Http.request
{ method = "POST"
, headers =
[ header "Origin" "http://elm-lang.org"
, header "Access-Control-Request-Method" "POST"
, header "Access-Control-Request-Headers" "X-Custom-Header"
]
, url = url
, body = body
, expect = Http.expectJson decoder
, timeout = Nothing
, withCredentials = False
}
tryRegister : Form -> (Result Http.Error JsonProfile -> msg) -> Cmd msg
tryRegister form msg =
let
url =
baseUrl ++ "register"
body =
encodeRegistration form |> Http.jsonBody
request =
tryPostRegistration url body profileDecoder
in
Http.send msg request
錯誤(榆樹客戶端):
HTTP404:NOT FOUND - 服務器沒有找到任何匹配 請求的URI(統一資源標識符)(XHR)選項 - http://localhost:5000/register
長頸鹿(服務器):
let configureCors (builder : CorsPolicyBuilder) =
builder.WithOrigins("http://localhost:5000").AllowAnyMethod().AllowAnyHeader() |> ignore
let configureApp (app : IApplicationBuilder) =
app.UseCors configureCors |> ignore
app.UseGiraffe webApp
let configureServices (services : IServiceCollection) =
...
services.AddCors |> ignore // Enables CORS
錯誤(長頸鹿服務器):
DBUG:Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware 1 OPTIONS請求不被支持
dbug:Giraffe.Middleware.GiraffeMiddleware [0] 長頸鹿迴歸斯內德對於一些HTTP/1.1 OPTIONS /註冊
附錄:
Elm Gateway (with CORS support)
Giraffe server (with CORS support)
Giraffe CORS sample project reference
我愛你的男人... –