Laravel - Prevent showing errors in the console

I am making an ajax request, and if the check fails, an error message is provided to the user.

However, the user may also see an error message in the console. For example, if the user does not fill out one of the fields in the console, this is what he shows:

Failed to load resource: server responded with status 422 (unprocessed entity)

I do not want them to see a mistake. How can I prevent them from seeing such errors, as well as another error in the console, for example, errors 501, etc. I just want to show the warning "An error has occurred." I do not want to show anything in the console.

My current AJAX code is:

(function ($) {
        $('#settingsForm').submit(function (e) {
            $.ajax({
                type: "POST",
                dataType: 'JSON',
                url: '/settings',
                data: $('#settingsForm').serialize(),
                beforeSend: function () {

                },
                success: function (data) {
                    message(data);
                },
                error: function (data) {

                    if (data.status === 422 ) {

                        var errors = $.parseJSON(data.responseText);
                        errorsHtml = '<div class="alert alert-danger"><ul>';
                        $.each(errors, function (index, value) {
                            errorsHtml += '<li>' + value + '</li>';
                        });
                        errorsHtml += '</ul></div>'
                        $('#status_settings').html(errorsHtml);
                    }

                }
            })
            ;

            e.preventDefault();
        })
        ;
    })
    (window.jQuery);
+4
2

, Laravel, 422 , AJAX. ( ) :

  • vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php 200 . /Http/Requests/Request.php.

    return new JsonResponse($errors, 422);
    

    :

    $errors['status'] = 'error'; return new JsonResponse($errors, 200);

  • Validator JSON 200 .

+4

, , 422 . . , 200 . , , , 200.

, , , , ( ). .

success: function (data) {

    if(data.response == 'success')
       message(data);

    else if(data.response == 'validation-error'){
        var errors = $.parseJSON(data.responseText);
        errorsHtml = '<div class="alert alert-danger"><ul>';
        $.each(errors, function (index, value) {
            errorsHtml += '<li>' + value + '</li>';
        });
        errorsHtml += '</ul></div>'
        $('#status_settings').html(errorsHtml);
    }

 },
0

Source: https://habr.com/ru/post/1618924/


All Articles