L
L
lemonlimelike2018-11-11 18:27:07
JavaScript
lemonlimelike, 2018-11-11 18:27:07

Why is the 419 error coming?

Hello! I am doing authorization on the project and faced the fact that when sending data, the server returns 419.
Here is my html code:

<div class="modal-dialog">
               	<div class="modal-header">
                	<h2>Авторизация </h2>
                    <label class="btn-close" for="modal-1" aria-hidden="true">×</label>
                </div>
                <div class="modal-body">
               		<input id="email" type="text" class="modal-body__text" placeholder="Email" required>
               		<input id="password" type="password" class="modal-body__text" placeholder="Password" required>
                </div>
                <div class="modal-footer">
                	<button type="button" class="btn btn-primary" onclick="auth();">Войти</button>
                </div>
            </div>

Here is the function that is responsible for the data transfer:
function auth(){
  		var email = document.getElementById('email').value;
  		var password = document.getElementById('password').value;
                var tokenHeader = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
          var postdata = new Headers({
                      'Content-Type': 'application/json',
                      'X-CSRF-TOKEN': tokenHeader
               });
  		var url = '{!!route('login')!!}';
  		fetch(url,{
  			method: 'POST',
  			postdata,
  			body: JSON.stringify({email: email, password:password, _token: '{!!csrf_token()!!}'})
  		}).then((response)=>{
  			return response.json();
  		}).then((data)=>{
  			console.log(data)
  		}).catch((error)=>{
  			console.log(error)
  		});
  	}

And here is the server response:
5be84a49c6f06351019583.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
netrox, 2018-11-11
@netrox

Are you sure you added a hidden field to the markup @csrf? The token must be sent in the Request body for each type of request (GET exception).

A
Anton, 2018-11-11
@Yadalay

Judging by your two screenshots in the comments, it is clear that the tokens are different. Judging by the js code, you are also passing 2 different tokens. Do this:
But, if you use a token in the header, then why do you specify it when transferring data?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question