fetch method simulates the request of the form with the help of the FormData object
this enables a complete HTTP request, and the back end can also receive data like a form
//
var formdata = new FormData();
formdata.append("username","lofayo");
fetch("web.php",{
method: "POST",
body:formdata
})
.then(res=>res.text())
.then(resTxt=>console.log(resTxt))
//
//web.php
$username = $_POST["username"]
post request in fetch mode, data or mock form. but , if you submit data directly like fetch official website , how can the backend receive it?
is not like a form, but a string, so how should the daemon receive it?
var url = "https://example.com/profile";
var data = {username: "example"};
fetch(url, {
method: "POST", // or "PUT"
body: JSON.stringify(data), // data can be `string` or {object}!
headers: new Headers({
"Content-Type": "application/json"
})
}).then(res => res.json())
.catch(error => console.error("Error:", error))
.then(response => console.log("Success:", response));