This answer may or may not help somebody else. But I had a similar problem with a jQuery ajax json parse error. My form wouldn't send an email and was returning the parse error via jQuery.
jQuery
$.ajax({
type: "POST",
url: "process-form.php",
data: dataString,
dataType:"json",
success: function(response) {
if(response.status == "success") {
} else if (response.status == "error") {
}
});
PHP
if (empty($name) || empty($email) || empty($phone) || !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email) || !preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
echo json_encode(array(
'status' => 'error'
//'message'=> 'error message'
));
} else {
$send = mail($sendTo, $subject, $message5, $headers, '‑[email protected]');
$sendText = mail($sendToPhone, '', $txtmsg, 'From: example <[email protected]>\r\n');
// insert into db
$formName = $_POST['name'];
$queryIn = "INSERT INTO database pseudo code";
//execute the query.
$result = mysqli_query($connection, $queryIn);
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
}
if($send){
echo json_encode(array(
'status' => 'success'
//This is the message the .ajax() call was looking for but it never posted because there was a MySQL error being printed to the browser along with it.
));
}
My issue was with my PHP page that the ajax call was going to (process-form.php). I was emailing the form data with the PHP mail() function and inserting the web form data into a MySQL Database. My PHP checks for mail() send success and prints the success message to the screen (which is what the jQuery .ajax() call is looking for. But my php/mysql was throwing an error and printing the error on the screen instead of the "success" message it was supposed to display. So my jQuery .ajax() call was trying to read a PHP json_encode() encoded array and it was getting the MySQL Error instead. Once I fixed my MySQL Error everything worked perfectly.
var str = $(this).serialize();adn in phpparse_str($_POST["data"],$array);header('Content-Type: application/json');?