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