R
R
Roman2019-02-12 17:27:30
JavaScript
Roman, 2019-02-12 17:27:30

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

3 answer(s)
R
Roman, 2019-02-13
@procode

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

M
Mikhail Vlasov, 2019-02-12
@Whiled0S

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);
})

D
Dmitry, 2019-02-13
@Astatroth

The documentation has information on how to attach a token to jQuery Ajax. Set once and forget.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question