The following JS code snippet:
var data_JSON = {
input: 'test',
message: 'Sending...'
};
$.ajax({
url: 'main_php.php',
type: 'POST',
data: data_JSON,
dataType : 'json',
contentType: 'application/json',
success: alert(data),
error: function (request, status, error) {
alert(error);
}
});
And the following associated PHP code (main_php.php):
if ($_POST){
$vals = array(
'input' => $input,
'message' => $message
);
header('Content-Type: application/json');
echo json_encode($vals);
}
Always result error (error
runs in $.ajax
), whatever I tried. In the browser's developer console, I could explore the complete length of the error message:
SyntaxError: Unexpected end of input at parse (native) at ajaxConvert ([...]/jquery-3.0.0.js:8544:19) at done ([...]/jquery-3.0.0.js:9011:15) at XMLHttpRequest. ([...]/jquery-3.0.0.js:9303:9)
Which is exactly the point where the JSON is failed to parse? What is the reason of this error and how can I solve it?
Note: JSON.stringify
did not work, if that helps anything.