Answer the question
In order to leave comments, you need to log in
How to embed csrf token in Ajax request (jQuery $.post() method)?
Hello.
When I try to send data via the $.post() method, the error
POST 405 Method not allowed is displayed in the console
, as I understand it, this is due to the fact that I did not insert the csrf token into the request?
But how to do it?
Thank you.
PS The question is a little covered in the documentation: https://laravel.com/docs/5.8/csrf#csrf-x-csrf-token but there is no for the $.post() method and then the postscript regarding "By default, the resources/ js/bootstrap.js file registers the value of the csrf-token meta tag with the Axios HTTP library. - confuses me)) Maybe there are some very simple ways?
Answer the question
In order to leave comments, you need to log in
In general, it turned out like this (everything works): I
registered it in HTML in accordance with the docs ( https://laravel.com/docs/5.8/csrf#csrf-x-csrf-token )
And in a script it looks like this:
$.post({
url: '/mypost',
data: {'_token': $('meta[name="csrf-token"]').attr('content'), 'somefield': somevalue}
}).done(function (data) {
alert('Успешно! ' + data);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question