我正在尝试使用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
发布于 2020-05-26 03:40:01
免责声明我是Thoth.Json的维护者。
看起来您使用的是2+版本的寓言。
fable-powerpack已经被弃用,并被分成不同的包。但是我们好像忘了更新这个网站了。
在使用Fetch API的情况下,它现在位于包Fable.Fetch中。
toJson已不复存在,建议使用Thoth.Json或Fable.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)
}https://stackoverflow.com/questions/61981877
复制相似问题