My PHP8 server is not receiving the data I am sending. When trying to insert a new song into my API, an error occurs and in the console, I see an object with POST as an empty array.
fetch("http://localhost/api.audio-player/",{
method: 'POST',
body:formData,
headers: {
"enctype":"multipart/form-data",
'Content-type': 'application/json; charset=UTF-8'
}
}).then(res=>(res.ok)?res.json():Promise.reject(res.statusText))
.then(data=>{
console.log(data);
}).catch(err=>{
console.error("Error: "+err||"Desconocido");
});
In my front-end code:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
if($_SERVER['REQUEST_METHOD']==="POST"){
echo json_encode(
array(
"POST"=>$_POST
)
);
}
?>
Note: Although I'm using $_POST in this example, I will also need to use $_FILES as I will be sending images and audio files. Also, I am utilizing XAMPP for this setup.