Answer the question
In order to leave comments, you need to log in
What is the best example from the web to use as a sample of working with Ajax (jQuery) from Laravel?
Hello!
I need a form via Ajax (jQuery) on Laravel to implement. I looked at examples on the net - and there who is in what much))) Who transmits the CSRF token "by hands" through headers, who else is what.
Please advise a 100% good example of Ajax-form implementation on jQuery in Laravel.
Thank you!
P.S. vue, please do not offer)) I basically want to deal with this, vue is an unfamiliar topic for me, but interesting, but right now there is simply no time for this.
Answer the question
In order to leave comments, you need to log in
Empirically, it was found that the implementation of Ajax using jQuery in the Laravel environment is no different from the implementation in the NOT Laravel environment))))))
The only thing you need to add to the form is this:@csrf
As for ajax requests, I would recommend the fetch API. This is a functionality built into javascript that allows you to make requests to the backend. Documentation
Example:
fetch('http://твой-url/', {
// Параметры ниже не обязательны, по умолчанию метод GET
method: 'POST', // любой твой метод
mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
// Здесь твои заголовки
"Content-Type": "application/json",
},
// Тут тело запроса с информацией, которую отправляешь серверу, переменная data – javascript-объект
body: JSON.stringify(data)
})
.then(response => {
// Тут пиши что делаешь после выполнения запроса
})
.catch(error => {
// Тут пиши обработку возникшей ошибки, пример ниже:
console.log(error);
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question