Cześć, próbuję wysłać dane POST w JS ale z jakiegoś powodu moje dane nie są przesyłane.
Mam taki kod JS:
function post(url, data)
{
let xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.send(JSON.stringify(data));
xhr.onload = function()
{
console.log(xhr.response);
}
}
let post_data = {action: 'test'};
post('http://localhost/ajax.php', post_data);
Zawartość ajax.php to:
<?
echo'Dane:'."\r\n\r\n";
print_r($_POST);
?>
A wynik który otrzymuję:
Dane:
Array
(
)
Z jakiegoś powodu mój kod nie przesyła argumentów w POST. W jaki sposób mogę naprawić ten problem?
