Kotlin / kotlin-wasm-examples

Examples with Kotlin/Wasm
https://kotl.in/wasm
Apache License 2.0
526 stars 60 forks source link

The request cannot be sent with the parameter ReqeustInit. #70

Open QingMingKt opened 2 months ago

QingMingKt commented 2 months ago

When sending a request using window.fetch, if you use ReqeustInit, you will not be able to send the request, even if RequestInit does not write anything.

(Because the picture cannot be uploaded, post the code.)

object ApiUtil {
    suspend fun login(username: String, password: String): ResultUDM<JsString> {
        val response = window.fetch(
            "http://127.0.0.1:9002/user/login", RequestInit(
                method = "POST", body = LoginUser(username, password)
            )
        ).await<Response>()
        return response.json().await()
    }
}

Browser console error:JsException: Exception was thrown while running JavaScript code

If the ReqeustInit parameter is removed, it can run normally.

Version information:

[versions]
androidx-lifecycle = "2.8.0"
compose-plugin = "1.6.11"
junit = "4.13.2"
kotlin = "2.0.20"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" }
androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }

[plugins]
jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }