我刚刚添加了x-editable到我目前的Laravel项目。它工作得非常好,但我有一个问题返回错误消息。Laravel - 如何返回json错误消息?
当控制器能够保存请求时,我得到'成功'! json消息。没关系。但是当我有一个错误时,我不会得到'错误!'信息。正如你所看到的,当$ article-> save()没有成功时,我激活了错误消息。
我在做什么错?
控制器:
$article->$input['name'] = $input['value'];
if($article->save()){
// this works
return response()->json(array('status'=>'success', 'msg'=>'Success!.'), 200);
}
else{
// this does not work
return response()->json(array('status'=>'error', 'msg'=>'Error!'), 500);
}
的JavaScript在浏览:
$(".xeditable").editable({
success: function(response) {
console.log(response.msg);
},
error: function(response) {
// console says, that response.msg is undefinded
console.log(response.msg);
}
});
亲切的问候。
你可以尝试打印回应吗? – AShly
您可以将此块放在try catch上,并在catch上返回错误响应。 – Matheus