I want to send a data object with fetch api with keepalive to the current page and get the data through php. how can I do it.
Data To Send
{
name: 'blabla',
age: 432,
type: 'pig'
}
I want to recieve as a post variable
$_POST['name'];
I’ve tried this but it is not working
fetch('', {
method: 'POST',
body: {name: 'blabla'},
keepalive: true
});
Sorry for no code
2
Answers
No idea what keepalive is, but if I want to send a fetch request to a php backend I would usually do something like this:
You could call fetch with json as content-type and use method POST as you allready tried, you also have to serialize the content on the body