1

I want to send a short form with email by ajax.

Code below adds a hidden input with name='_token'

{!! Form::open(['route'=>'registerCheck', 'id'=>'register_form', 'novalidate'=>'novalidate']) !!}

In js script I add data to request:

$.ajax({
    method:'POST',
    url: $form.attr('action'),
    data:{
        '_token': $form.find('[name="_token"]').val(),
        email: $('#email').val(),
        user: $this.attr('id'),
    }
})  

How can I authenticate it and which namespaces should I include to do this?

1
  • Change method 'POST' to 'GET' Commented Sep 22, 2016 at 13:37

1 Answer 1

1

First add this meta

<meta name="csrf-token" content="{{ csrf_token() }}" />

then in your script add this

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

finally change your request to this

$.ajax({
method:'POST',
url: $form.attr('action'),
data:{
    _token: CSRF_TOKEN,
    email: $('#email').val(),
    user: $this.attr('id'),
   }
})  
Sign up to request clarification or add additional context in comments.

6 Comments

Thtat's not accualy what i asked. I want to know, what to do with the _token on server side. Mayby it is automatically authenticated with some service
You don't have to The VerifyCsrfToken middleware, which is included in the web middleware group, will automatically verify that the token in the request input matches the token stored in the session. @julew
Ok thanks. But now i am wondering, how it's done, that the middleware group is automaticly added to each request. I mean in which file i cant find this notation.
Illuminate\Foundation\Http\Middleware\VerifyCsrfToken this is the class handles the requests
But where it's registered to all http requests. Normally, when i want to add middleware to route, a have to add this in route file. I didn't add mentioned middlewarem, but it works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.