0

I am making a request using ajax and laravel 9 to check the input of a field, but laravel returns the field error as error 422, which is not a big problem because I can work with it in ajax, but even using the error function it shows up in the console. How can I make the error not show up in the console?

Laravel:

public static function changeUsername(Request $request){

        $request->validate([
            'username' => 'required|string|max:255|unique:users',
        ], [
            'username.required' => 'O username não pode estar vazio',
            'username.string' => 'O username não pode conter apenas números',
            'username.max' => 'O username não pode ter mais de 255 caracteres',
            'username.unique' => 'O username já está em uso',
        ]);
        
        $user = User::find(Auth::user()->id);
        $user->username = $request->username;
        $user->save();
        
        return response()->json([
            'message' => 'Username alterado com sucesso!',
            'user' => $user
        ], 200);
    }

Ajax:

 function saveUsername(username) {
        $.ajax({
            type: "POST",
            url: "{{ route('user.updateUsername') }}",
            data: {
                username: username,
                _token: "{{ csrf_token() }}"
            },
            success: function(response) {
                $('#username').children().text(username);
                showToast("Sucesso", "Username Alterado com Sucesso");
                $('.usrChangeBtn[function="load"]').hide();
                $('.usrChangeBtn[function="edit"]').show();
            },
            error: function(response) {
                showToast("Erro", response.responseJSON.message);
                $('.usrChangeBtn[function="load"]').hide();
                $('.usrChangeBtn[function="edit"]').show();
            },
            beforeSend: function() {
                $('.changeUsername').hide();
                $('#username').show();
                $('.usrChangeBtn[function="load"]').show();
            }
        });
    }
redystum
  • 352
  • 3
  • 10
  • Or more precise this answer: [Hide 401 console.error in chrome dev tools getting 401 on fetch() call](https://stackoverflow.com/questions/41515732/hide-401-console-error-in-chrome-dev-tools-getting-401-on-fetch-call) --- it can't be done. See accepted answer – Uwe Dec 29 '22 at 17:34
  • The google search gives you a lot of similar results. [see](https://www.google.de/search?q=do+not+display+jquery+ajax+error+in+console+site:stackoverflow.com&sxsrf=ALiCzsYUBb-qd5suWHabUW7pjdeItNC-Yw:1672335110401&sa=X&ved=2ahUKEwiTpc3hrZ_8AhWjRvEDHd-gB2IQrQIoBHoECAoQBQ&biw=1440&bih=768&dpr=2) – Uwe Dec 29 '22 at 17:36
  • Even with try catch it goes to the console, but ok, I understand that these network errors always go to the console. Thanks @Uwe – redystum Dec 29 '22 at 17:40

0 Answers0