如何使用 fetch API 发送 post 请求

how do I send post request with fetchAPI

我在 index.html 中有一个简单的 'username' , 'password' 表单,问题是我想向 'login.php' 发送一个 post 请求(目前仅回应 'hello'),但我发现的所有有关 fetchAPI 的示例都与 JSON 相关。在我的一个项目中,我想将 post 用户名更改为 'usercheck.php' 之类的内容,并检查可用的用户名并使用 fetchAPI 写入 'This username is available' '...不可用'。我可能缺少语法...
所以问题是我不断收到 405(方法不允许)

main.js

const myForm = document.querySelector('#myForm');

myForm.addEventListener('submit', (e) => {

e.preventDefault();

const username = document.querySelector('#inp_username').value;
//const password = document.querySelector('#inp_password').value;

fetch('login.php' , {
    method: 'POST', 
    body: {username: username}
})
.then((res) => res.text())
.then((data) => {
    console.log(data);

})
})

和我的login.php

<?php

echo 'hello';

?>

您的后端不允许您使用 POST。确保在您的后端有一个允许 POST 的路由。