I have looked around and followed the instructions on this post but I still can not get the error
function to execute. What I want to do is have my PHP script return either error
or success
along with a message. Eventually it will be data returned from a database that will be put inside of a div
but for now I just need to get the error handling working. I am hoping someone here can help me out with this. I have included my code below.
This is obviously my AJAX request.
function getProductInfo() {
if($("#serialNumber").val() != '') {
serialNumber = $("#serialNumber").serialize();
$.ajax({
type: "POST",
url: 'post.php',
data: serialNumber + "&getSerialNumber=" + 1,
dataType: 'json',
success: function(data, textStatus, jqXHR) {
console.log("SUCCESS");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("ERROR");
}
});
}
}
This is my php function that will return the error and message as JSON
function getSerialNumber() {
$serial = $_POST['serial'];
$product = new Inventory;
if($product->GetSerial($serial)) {
$productInfo = $product->GetSerial($serial)->first();
echo '{"error": false, "message": "Successfully got serial number"}';
} else {
echo '{"error": true, "message": "failed to get serial number"}';
}
}
As the current code stand it will only keep outputting SUCCESS
regardless if it actually has an error or not.