0

我正在尝试按照本教程使用 Fable 进行 POST 请求:https ://fable.io/fable-powerpack/posts/fetch.html

我收到此编译错误:

error FSHARP: The value or constructor 'toJson' is not defined. Maybe you want one of the following:
       JSON (code 39)
     @ ./UI/UI.fsproj 1:0-24 1:0-24

这是我的代码:

let submitForm = 
    let txtName = getInputElementById("txtName")
    let postData = { Name = txtName.value }

    let requestProps =
        [ RequestProperties.Method HttpMethod.POST
        ; requestHeaders [ContentType "application/json"]
        ; RequestProperties.Body (unbox (toJson postData))
        ]
        //Encode.Auto.toString(1, postData)
        //toJson    
        //Fable.Core.JS.JSON.stringify

    promise {
        let! response = fetch "http://localhost:5000/employee/create" requestProps
        let responseText = response.text()
        Browser.console.log responseText
    } |> ignore

我还在 Fable/.fable/Thoth.Json.2.0.0/Encode.fs 中得到这些编译错误:

The value, constructor, namespace or type 'Array' is not defined. (code 39)
The value, constructor, namespace or type 'JSON' is not defined. 

我猜这是因为 Fable 引用的是 Thoth 2.0.0,而不是最新版本。我该如何解决这个错误?

https://github.com/jaydpather/FunctionalCrudForms/blob/master/Fable/UI/UI.fs

4

1 回答 1

1

免责声明我是 Thoth.Json 的维护者。

好像您使用的是 2 岁以上的 Fable 版本。

fable-powerpack 已被弃用,并已拆分为不同的包。但是我们好像忘记更新这个网站了。

对于 Fetch API,它现在位于包 Fable.Fetch 中。

toJson不再存在,建议使用Thoth.JsonFable.SimpleJson

还有一个包Thoth.Fetch已经将 Fetch 和 Thoth.Json 粘合在一起,因此您不必编写(反)序列化部分。

在你的情况下,它会给出类似的东西:

let createBook () : JS.Promise<unit> =
    promise {
        let url = "http://localhost:5000/employee/create"
        let txtName = getInputElementById("txtName")
        let data =
            {| Name = = txtName .Value |} // Using an anonymous for simplicity

        return! Fetch.post(url, data, caseStrategy = CamelCase)
    }
于 2020-05-25T19:40:00.617 回答