Hi,
ich versuche Daten per AJAX zu versenden.
Leider gelingt es mir nicht, die Daten mit PHP anzunehmen.
// Sammle alle Klassen
const words = document.querySelectorAll('.ocrx_word');
// Stelle ein leeres Array bereit
const datasBooks = [];
// Suche alle Wörter und packe sie in ein Array
words.forEach((wordItems) =>
{
let word = wordItems.innerText;
datasBooks.push(word);
});
// Go-AJAX
const xhr = new XMLHttpRequest();
// Zielroute
xhr.open("POST", "actions/test.route.php", true);
// Antwort
xhr.onreadystatechange = function ()
{
if (xhr.readyState==4 && xhr.status==200)
{
console.log('xhr > Senden > ok');
}
};
//Header
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
// Ciao
xhr.send(datasBooks);
console.log(datasBooks);
In der Zielroute versuche ich:
$wordsInBook = json_decode($_POST['datasBooks']);
Ich bekomme die Fehlermeldung:
<b>Warning</b>: Undefined array key "datasBooks"
Nur, warum wird der Key nicht gefunden?