T
T
TyllerDurden2021-10-27 15:59:11
AJAX
TyllerDurden, 2021-10-27 15:59:11

How to correctly pass using fetch method POST?

Below is an example of my code. I cut it down to the point so as not to load it.

let formData = new FormData();
formData.set('titlePost', 'Какой-то title');
formData.set('content', 'Какой-то content');

let promise = fetch('../ajax/sendpost.php', {
            method: 'POST',
            body: formData,
        });

        promise.then(
            response => {
                return response.text();
            }
        ).then(
            text => {
                console.log(text);
            }
        );


What should I write in php file to get titlePost and content from formData? $_POST['titlePost'] is empty. var_dump(file_get_contents(' php://input ')) doesn't output anything either.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nadim Zakirov, 2021-10-27
@zkrvndm

Not set, but append must be written! Replace set with append.

A
Alexander, 2021-10-27
@Aleksandr-JS-Developer

Network tab in devtools. See if you are actually sending what you want to receive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question