I have a click event where I compose a json data, then I want to POST it to a PHP file for processing. But something goes wrong. My PHP file is simplified for now looking like this:
<?php
header('Content-Type: application/json');
var_dump($_POST);
?>
And the code for POST-ing looks like this:
// myarray is: var myarray = new Array();
// and it gets populated above this code
var strObj = JSON.stringify(myarray);
alert(strObj); // so far I get the alert containing valid JSON text
$.ajax ({
type:"POST",
url:"proces.php",
contentType: "application/json",
dataType: "json",
async: false,
data: strObj,
success: function(){ alert("success")},
error: function(){ alert("error")}
});
So when I click the button, I receive the alert containing the JSON string (looks fine), then I get the alert saying "error", and when I check the console for the response of proces.php all I see is:
array(0) {
}
What am I doing wrong? What can I do to make it right?
console.log(this, arguments);
to error callback and check in console what exact error you get; also you have typo insuccess
param name. – Tommi Jul 11 at 11:55