Support the ongoing development of Laravel.io →
Forms Validation Laravel.io
Last updated 1 year ago.
0

I was trying to fix this and I thing it's csrf token fault, in my form I using this:

{!! csrf_field() !!}

and it was working while I not start using jQuery, but now I don't know how to send csrf token through jQuery $.post please help me to do this.

0

A great method is to create a meta element in the <head> of your HTML:

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

In you jQuery $.post code you would then reference the CSRF token like this:

'_token': $('meta[name="csrf-token"]').attr('content')
0

To find out if your jQuery Post method is failing you can use jQuery Deferred objects which are deferred.done() & deferred.fail().They will be helpful in situations like yours. If you see the jQuery Post tutorial which explains how to use deferred objects, then your code will become:

$.post("{!! route('register') !!}", $("#reg_form").serialize())
.done(function (result, status, xhr) {
    alert(result)
})
.fail(function (xhr, status, error) {
    alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
});

Here basically i have removed your callback function and placed the .done() and .fail(). Inside the .done() you can call any function to do your specific task.

If the jQuery Post is not working property you will get the alert given at .fail() and you will know what caused the problem.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.