view raw
김정명 김정명 - 6 months ago 30
Javascript Question

laravel ajax validation how to make if it's failed?

I want to make if any values are nothing, show error message box.

is working successfully, but I have troubles in
if its failed
brings nothing. How can I solve this problem?
i have tried
$errors = $validator->errors();
$errors = json_decode($errors);


url: url,
type: 'POST',
data: $writeForm.serialize(),
error: function(res){
if (!res.status) {

var error = $.parseJSON(res.message);
$('div.visitor-form > div.alert').html(error);




$validator = validator::make($data = Input::all(), mypage::$rules);
if($validator->fails()) {

$response = array();
$response['status'] = false;

$errors = $validator->getMessageBag()->toArray();
$response['message'] = $errors;

return response()->json($response);


$response['errors'] = $validator->errors()->all();

return response()->json($response, 400);

The browser needs to know the error status in order to trigger your error method in that ajax function. See

        url: url,
        type: 'POST',
        data: $writeForm.serialize(),
        error: function(res){ //by using this method, you're assuming that all the logic here is for error handling.
                console.log(res.status, res.errors);