I'm having trouble with the following JavaScript, where data is a JavaScript object:
var ajax_send = function(data) {
var httpRequest;
makeRequest('/prototype/test.php', data);
function makeRequest(url, data) {
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(JSON.stringify(data));
}
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
};
The PHP is:
$data = $_POST['data'];
$obj = json_decode($data);
echo $obj;
In dev tools the request payload looks ok but it doesn't seem to be what the PHP is looking for. Nothing gets through to the PHP script and the response is empty.
What am I doing wrong?