jquery - $.ajax error cannot catch thrown exception from php -
$.ajax({ type: "post", url: "functions/add_vendor.php", data: { vendor_info_json : vendor_info_json }, success:function(result) { alert('successfully done.') location.reload(); }, error: function(jqxhr, textstatus, errorthrown){ alert('error message: '+ textstatus); alert('http error: '+ errorthrown); } })
this jquery code i'm working on. , following add_vendor.php file being called.
<?php include_once('../models/vendor_db_manager.php'); $vendor_info_json = $_request["vendor_info_json"]; $db = new vendor_db_manager(); $db->connectto('bby'); try { //this returns false test code. if($db->insertdatatotable('mfvendor', $vendor_info_json)) { $db->disconnect(); header('content-type: application/json'); $data = json_encode($result); echo $data; } else { throw new exception("error has been detected."); } } catch (exception $e) { header("http/1.1 500 internal server error"); echo "exception occurred: " . $e->getmessage(); } ?>
ok, 'if($db->insertdatatotable('mfvendor', $vendor_info_json))' part false test $.ajax's error handling code. no matter do, error: callback not working if php code throws exception time.
what problems i'm missing here? :(
when server throws internal server error, javascript side it's still success. in success
function have check response's status code, , handle accordingly.
Comments
Post a Comment