I am trying to send an objet from javascript to php via ajax. My json seems correct and the php receives as far as I can tell what It should, yet I can't decode the object in php.
My js
const params = 'data=' + JSON.stringify(data)
console.log(params)
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.open('POST', request, true);
xhr.onreadystatechange = function() {
if(xhr.status==200){
if(xhr.readyState>3) success(xhr.responseText);
}
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(params);
What my js in sending : data={"post_type":"post","tax_query":{},"meta_query":{"category":4}}
My php
$data = $_POST['data'];
var_dump($data);
$data = html_entity_decode($data);
var_dump($data);
$data = json_decode($data);
var_dump($data);
What my PHP is receiving in $_POST['data']
: string(73) "{\"post_type\":\"post\",\"tax_query\":{},\"meta_query\":{\"category\":4}}"
Yet when I try to json_decode
I get NULL
I know there are a lot of posts on this already but I have seemed to have tried them all to no prevail. What am I doing wrong?