你如何实现获取api?

How do you implement the fetch api?

我不明白,如何在kotlin中实现fetch api 我的代码:

var smf: dynamic = js("({})")
smf.method = "GET"
smf.mode   = "cors"
smf.cache  = "default"

window.fetch(url, smf)
        .then({response -> {
            console.log("response")
        }})
        .catch({error ->
            console.error("error")
        })

而且根本不起作用。没有控制台消息和任何

我猜问题出在您的第一个 lambda 表达式中:

.then({response -> {
    console.log("response")
}})

此代码不执行任何操作,因为它等同于:

.then(fun(response: dynamic){
    return {console.log("response")}  // creates a lambda and returns it for no reason
 })

TL;DR 要修复代码,请删除第二对大括号:

.then {response -> console.log("response")}