使用ajax
我正在更新一些值,并且作为响应,我显示toaster
红色(表示错误)和绿色(表示成功),但是问题是,如果出现错误,则始终显示绿色烤面包机。我想要出现错误的红色烤面包机,这是我已经尝试过的代码
js
success: function (data) {
toastr.success(data.message);
},
error: function(err) {
toastr.error(err.message);
}
控制者
if (some condition){
$customer->save();
return response()->json([
'success'=> true
'message' => 'User status updated successfully.'
]);
} else {
return response()->json([
'error'=> true,
'message'=> 'Visit Failed distance is too long'
]);
// this should be red toast but it green
}
请阅读上面代码的注释,// this should be red toast but it green
有人可以帮助我如何在出错时显示红色烤面包机?
成功:如果Ajax请求成功完成,则调用成功函数。即,如果服务器返回的HTTP状态为200 OK。
错误:如果服务器响应HTTP错误,则执行错误功能。因此,如果您遇到错误,err.message
则将在此处无法辩解。因此,请改写自定义消息:
success: function (data) {
if(data.success == true){
toastr.success(data.message);
} else {
toastr.error(err.message);
}
},
error: function(err){
// Your Error Message
toastr.error("Error with AJAX callback !");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句