小编obe*_*bei的帖子

如何在 WebAssembly 中使用 web-sys 发出带有 JSON 正文的 POST 请求?

如何在 WebAssembly 中使用 web-sys 创建带有 JSON 主体的 POST 请求?

下面这个例子展示了如何发出 GET 请求,我需要更改opts.method("GET"); opts.method("POST"); 但我如何将 JSON 主体传递给 reqeuest.

    let mut opts = RequestInit::new();
    opts.method("GET");
    opts.credentials(web_sys::RequestCredentials::Include);
    
    let request = Request::new_with_str_and_init(
        "http://localhost:8080/api/v1/books",
         &opts
    ).unwrap();
    
    match web_sys::window() {
        Some(window) => {
            let _res = JsFuture::from(window.fetch_with_request(&request))
                .await
                .map(|err| web_sys::console::log_1(&format!("{:?}", err)
                .into()));
        },
        None => web_sys::console::log_1(&format!("window is none").into()),
    }
Run Code Online (Sandbox Code Playgroud)

rust webassembly wasm-bindgen yew web-sys

2
推荐指数
1
解决办法
2191
查看次数

标签 统计

rust ×1

wasm-bindgen ×1

web-sys ×1

webassembly ×1

yew ×1