Lorsque j'essaie l'ajax dans la même page pour le html cela fonctionne. Comme ça;Django 403 Erreur Interdite
<html>
<head>
...
</head>
<body>
....
<script>
$.ajax({
url: /test/,
method: 'POST',
headers: {'X-CSRFToken': '{{ csrf_token }}'},
data: { name: a, surname: b},
dataType: 'json',
success: function (data) {
getList(data);
}
});
</script>
</body>
</html>
Lorsque j'essaie l'appel même javascript comme externe. Ça ne marche pas. Pourquoi?
<html>
<head>
...
</head>
<body>
....
<script src="{% static 'js/test.js' %}"></script>
</body>
</html>
Êtes-vous configurer le fichier statique correctement dans django? –
fichier statique correctement configuré. J'ai 403 erreur. – Halim
[Les docs ont une solution pour cela] (https://docs.djangoproject.com/fr/1.11/ref/csrf/#setting-the-token-on-the-ajax-request) qui ajoute automatiquement le jeton CSRF à tous les appels AJAX. – dspacejs