[英]how to get success or failure response from controller to ajax in laravel?
i want to show toaster on success or failer so i tried this code我想在成功或失败时显示烤面包机,所以我尝试了此代码
$.ajax({
type: "GET",
dataType: "json",
url: '{{ route('users.update.visit_clear') }}',
data: {'visit_clear': visit_clear, 'user_id': userId , 'location': location , 'cs_location': cslocation },
success: function (data) {
if (data.message != ""){
toastr.success(data.message);
}
}
error: function (data) {
toastr.error(data.message1);
}
});
and in controller i have condition在控制器中我有条件
if($var <= 1){
return response()->json(['message' => 'User status updated successfully.' ]);
}
else{
return response()->json(['message1' => 'Visit Failed Distance is too long' ]);
}
if i use error:function it doesnot response me如果我使用 error:function 它不会响应我
To be in the error
ajax callback you have to return an http error code (ex: 500, 400 etc).要在error
ajax 回调中,您必须返回一个 http 错误代码(例如:500、400 等)。
You can check the different http error codes here https://en.wikipedia.org/wiki/List_of_HTTP_status_codes .您可以在此处查看不同的 http 错误代码https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 。
You can add a status code
as a second argument in your else
response您可以在else
响应中添加status code
作为第二个参数
ex: return response()->json(['message1' => 'Visit Failed Distance is too long' ],500);
例如: return response()->json(['message1' => 'Visit Failed Distance is too long' ],500);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.