php - Laravel - How to return json error message on error? -
i added x-editable current laravel project. works have problem returning error message.
when controller able save request, 'success!' json message. that's fine. when have error, not 'error!' message. can see fire error message when $article->save() not successful.
what doing wrong?
controller:
$article->$input['name'] = $input['value']; if( $article->save() ){ // works return response()->json(array('status'=>'success', 'msg'=>'success!.'), 200); } else{ // not work return response()->json(array('status'=>'error', 'msg'=>'error!'), 500); }
javascript in view:
$(".xeditable").editable({ success: function(response) { console.log(response.msg); }, error: function(response) { // console says, response.msg undefinded console.log(response.msg); } });
kind regards.
i'm not familiar x-editable
try change response code in case of error 500
200
, in javascript
$(".xeditable").editable({ success: function(response) { if (response.status == 'error') { console.log('error: ' + response.msg); } else { // stuff successful calls console.log('success: ' + response.msg); } }, error: function(xhr, status, error) { console.log('server error: ' + status + ' ' + error); } });
Comments
Post a Comment