I need to send a simple object from my js script to php.
I created a js like this:
var data = JSON.stringify({
"name": "myname",
"email": "foo@bar.com",
"age": "15"
});
var http = new XMLHttpRequest();
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.send(data);
http.onreadystatechange = (function(e){
if (http.readyState == 4 && http.status == 200) {
console.log(JSON.parse(http.responseText));
}
});
Something does not work properly.
My PHP script just simply returns $_POST array back to js script.
echo json_encode($_POST);
And the response is weird:
{"name": "myname","email": "foo@bar.com","age": "15"}: ""
It looks like server treats my whole object as a single parameter.
What can cause such behavior?