I have searched the internet and stackoverflow topics for an answer but I have not found any. I know JQuery works fine with sending the object but when I try to do the same without the framework, my post is not going through to the PHP page.
My basic 2 basic functions are:
function sendAjax(){
let car = {type:"Fiat", model:"500", color:"white"};
var myURL = "ajaxpost.php";
var post_data = {
myData: car
};
ajax(myURL, post_data);
}
function ajax(url, post_data) {
var params = typeof data == 'string' ? post_data : Object.keys(post_data).map(
function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(post_data[k]) }
).join('&');
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function(){
if(this.status == 200){
document.write(this.responseText);
}
}
xhr.send(params);
}
My PHP page at ajaxpost.php contains the following
<?php
$data= $_POST['myData'];
$results = print_r($data, true);
echo $results; // just gives me [object Object]
?>