I have following code:
use std::time;
use actix_web::{
middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder, Result,
get,
post,
http::{
header::{self ,ContentType},
Method, StatusCode,
}
};
use actix_files::Files;
use serde::{Serialize, Deserialize};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(get_html)
.service(show_result)
})
.bind(("127.0.0.1", 8081))?
.run()
.await
}
#[get("/")]
async fn get_html() -> impl Responder {
HttpResponse::Ok()
.content_type("text/html")
.body(include_str!("../../form2.html"))
}
#[post("/result")]
async fn show_result(params: web::Form<InputCodes>) -> impl Responder {
let res = format!("your input is {}, and I have got it in {:?}", params.name, time::Instant::now());
HttpResponse::Ok()
.content_type("text/palin")
.body(res)
}
#[derive(Serialize, Deserialize)]
pub struct InputCodes {
pub name: String
}
And the form2.html
contains these:
<!doctype html>
<html>
<head>
<title>Online Strategy</title>
</head>
<body>
<form action='/result' method=POST>
<textarea id="editor" name="name"></textarea>
<button type=submit>Run</button>
</form>
<textarea id="editor2" name="nam2"></textarea>
</body>
</html>
The process works as following:
- I open the url
127.0.0.1:8081
, it show this web page
- I click the Run button, then the page jump to
127.0.0.1:8081/result
, which showasync fun show_result
's result. - I am trying to show the result(from step 2) in the second textarea(instead of
127.0.0.1:8081/result
page), like this
How can I define the show_result
function to get it?