我正在尝试使用Fetch API创建注册表单。表单数据已发送到数据库,但是在devtools'POST http://localhost/sem2/UX2/ws.php?request=registration 401(Unauthorized)中仍然出现错误。尝试了几种不同的标题,但到目前为止没有任何效果。 这是我的JS代码-
document.querySelector('#regoform').addEventListener('submit', e => {
e.preventDefault();
let form = document.querySelector('#regoform');
const data = new URLSearchParams();
for(const p of new FormData(form)) {
data.append(p[0], p[1], p[2], p[3], p[4], p[5]);
}
fetch('./ws.php?request=registration', {
method: 'POST',
body: data,
mode: 'cors'
})
.then(response => response.json())
.then(response => console.log(response))
.catch(error => console.log(error))
});
这是我的php文件
if (isset($_GET['request'])){
switch($_GET['request']) {
case 'registration':
$fName = $_POST['fName'];
$surname = $_POST['surname'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$email = $_POST['email'];
$password = $_POST['password'];
$result = $dbobj->newUser($fName, $surname, $gender, $age, $email, $password);
break;