I'm having trouble showing a custom error in a php response sent by ajax.
Suppose this call Ajax:
var uri = "http://localhost/aniadir.php"
$.ajax({
url: uri,
data: data,
method: "POST",
dataType:"json",
async: true,
success: function (response) {
console.log("OK" + response.data.message);
},
error: function (xhr, textStatus, errorMessage) {
console.log("ERROR" + errorMessage + textStatus + xhr);
}
}); // end ajax
From PHP I forced an error in the connection to the DB:
$mysqli = new mysqli('localhost', 'root', 'error', 'tabla');
$jsondata["success"] = false;
$jsondata["data"] = array(
'message' => $mysqli->error
);
header('Content-type: application/json; charset=utf-8');
echo json_encode($jsondata, JSON_FORCE_OBJECT);
If the answer is success, the message is correctly displayed by response.data.message
The problem is how to display the message created in PHP when it is an error.