如何在WebAssembly中使用Web-Sys与JSON主体提出发布请求?
如何使用WebAssembly中的Web-Sys使用JSON主体创建发布请求?
下面的示例显示了如何提出请求,我需要更改 opts.Method(“ get”); opts.method(“ post”); ,但是如何我将json尸体传递到了重新界面。
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()),
}
How to create POST request with JSON body using web-sys in WebAssembly?
This example below showing how to make GET request, I need to change opts.method("GET"); to opts.method("POST"); but how can i pass a JSON body to the 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()),
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 noreferrer“>
repess unpessignit: :body()
以及使用标题:: set
。您必须将选项< jsvalue>
传递给request> requestInit :: body()
。要通过字符串,您可以执行:要发送更复杂的对象,可以使用
jsvalue :: from_serde
。You can set the body using
RequestInit::body()
and the required headers usingHeaders::set
. You have to pass anOption<JsValue>
toRequestInit::body()
. To pass a string, you can do:To send a more complex object, you can use serde with
JsValue::from_serde
.