Answer the question
In order to leave comments, you need to log in
Why are POST parameters not being passed?
I pass POST parameters, but PHP does not see them, it displays only a test string. Tried with GET, everything worked as it should.
JS
fetch('test.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ code }) // { code:string }
})
<?
echo 'Test'; // Тестовая строка, выводится
echo $_POST['code']; // Не выводится
fetch( 'test.php?' + new URLSearchParams({ code }) );
<?
echo $_GET['code']; // Работает
Answer the question
In order to leave comments, you need to log in
Because the variable $_POST
can only work with application/www-x-form-urlencoded and multipart/form-data . The custom type Body as JSON already needs to be obtained and parsed by handles.
<?php
$json = json_decode(file_get_contents('php://input'), true);
var_dump($json);
$_POST
, you need to pass the request body through FormData , instead of JSON.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question