I have the following html code:
<!doctype html>
<html>
<head>
<title>Online Strategy</title>
</head>
<body>
<iframe name="dummyframe" id="dummyframe" style="display: none;"></iframe>
<form action='/result' method=POST target="dummyframe">
<textarea id="editor" name="name"></textarea>
<button type=submit>Run</button>
</form>
<textarea id="editor2" name="nam2"></textarea>
<script>
function GetData() {
console.log("my ssss");
fetch("/result")
.then((response) => {
var upperCase = response.json();
console.log("dddd");
var upperCase = upperCase.toUpperCase();
document.getElementById("outputText").innerHTML = upperCase;
})
}
</script>
</body>
</html>
What I am trying to do is as following:
- I input some text in the first textarea, then I click the Run button, then in url 127.0.0.1/result, will have a respone
- I fetch the respone, and paste the body into the second textarea.
But it doesn’t work as expected, it has error 404 not found
.
What am I doing wrong?
The server side is like this:
#![allow(unused_imports)]
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 mut res = format!("your input is {}, and I have got it in {:?}", params.name, time::Instant::now());
HttpResponse::Ok()
.content_type("text/plain")
.body(res)
}
#[derive(Serialize, Deserialize)]
pub struct InputCodes {
pub name: String
}
which is some rust codes. The function show_result handle the form post, and write some word into the /result webpage.