Taylor Swift Taylor Swift - 1 year ago 65
Javascript Question

Laravel - Prevent errors from being shown in console

I'm doing an ajax request and if the validation fails, the user is presented with an error message.

However, the user can also see the error message in console. For example, if the user doesn't complete one of the fields, in console, this is what it shows:

Failed to load resource: the server responded with a status of 422 (Unprocessable Entity)

I do not want them to see the error. How can I prevent them from seeing errors like such and also other error in console, like 501 errors and stuff. I just simply want to display an alert saying "An error occurred". I don't want to show anything in console.

My current AJAX code:

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

success: function (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>'



Answer Source

It looks like you are using Laravel Form Request validation which sends a 422 status code for failed validation along with errors in case if AJAX form submission. There are two things you can do now to change the status code and remove the error message ( only in this case ) from the console:

  1. Override the response function in vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php and change the status code to 200 in your new function. Copy the function from there and paste it in Request class in app/Http/Requests/Request.php. Change the following line

    return new JsonResponse($errors, 422);

    to something like this:

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

  2. Instead of using Form Request Validation, create a Validator instance for validation in your controller functions and return JSON response upon failed validation with 200 status code.