X-CSRF-Token to access the REST API

Hello everyone, I am looking to recover the X-CSRF-Token to have access to the REST API, but with my code I have a 401 error. can you help me

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

        <script>

            function sendAjaxRequest() {

                const data = {

                    username: "admin",

                    password: "admin"

                };

                return new Promise((resolve, reject) => {

                    $.ajax({

                        url: 'http://localhost:8080/axelor-erp-6.4.14/login.jsp',

                        type: 'POST',

                        contentType: 'application/json',

                        data: JSON.stringify(data),

                        success: function(response, textStatus, jqXHR) {

                            const token = jqXHR.getResponseHeader('X-CSRF-Token');

                            // Faites ce que vous voulez avec le token ici

                            console.log('Token:', token);

                            resolve(token);

                        },

                        error: function(xhr, status, error) {

                            console.log('Erreur lors de la requête. Statut:', xhr.status);

                            console.log('Réponse serveur:', xhr.responseText);

                            reject(error);

                        }

                    });

                });

            }

            // Appeler la fonction pour envoyer la requête AJAX

            sendAjaxRequest()

                .then(token => {

                    $.ajax({

                        url: 'http://localhost:8080/axelor-erp-6.4.14/ws/rest/com.axelor.apps.base.db.Partner/search',

                        type: 'POST',

                        headers: { 'X-CSRF-Token': token },

                        data: JSON.stringify({

                            fields: ["fullName"],

                            limit: 20

                        }),

                        contentType: 'application/json'

                    }).done(response => {

                        const productNames = response.data.map(e => e.fullName);

                        const productList = $('#product-list');

                        console.log(productList);

                        productNames.forEach(name => {

                            $(`<li>${name}</li>`).appendTo(productList);

                        });

                    });

                })

                .catch(error => {

                    console.error(error);

                    // Gérer les erreurs ici

                });

Hello,

I give an answer here.
Try an ask me again if you need it

I already looked at this subject, but I don’t use php and it doesn’t give me what I need. can you help me

hmmm j’utilise pas php…

tu récupère d’abord les cookies

POST http://site/login.jsp
BODY { « username »: « demoerp », « password »: « demoerp » }
Headers { « Content-Type »: « application/json » }

Ensuite la requete

GET http://site/ws/rest/com.axelor.apps.sale.db.SaleOrder

HEADER { « Content-Type »: « application/json », « Cookie »: « [CSRF-TOKEN=99999999999999999999999; Path=/open-suite-fr; SameSite=None; Secure; JSESSIONID=ABCD; Path=/open-suite-fr; Secure; HttpOnly; SameSite=None] » }

I use a post method you can see my code on my post if you want.