From 6600c9ab139586022bbc3981a7ddba95edee1073 Mon Sep 17 00:00:00 2001 From: Francesco Lorenzo D'Amico Date: Tue, 20 May 2025 15:53:15 +0200 Subject: [PATCH] Add AntiForgeryToken with them stuff on js request --- Francesco.Recipes.World/Views/Recipe/Details.cshtml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Francesco.Recipes.World/Views/Recipe/Details.cshtml b/Francesco.Recipes.World/Views/Recipe/Details.cshtml index 4ea25c7..06fe787 100644 --- a/Francesco.Recipes.World/Views/Recipe/Details.cshtml +++ b/Francesco.Recipes.World/Views/Recipe/Details.cshtml @@ -27,6 +27,7 @@

Ingredients

+ @Html.AntiForgeryToken()
    @foreach (var ingredient in Model.RecipeIngredients) { @@ -48,10 +49,14 @@ var formData = new FormData(form); var selectedIngredientIds = formData.getAll('ingredientIds'); + + var token = document.querySelector('input[name="__RequestVerificationToken"]').value; + var response = await fetch('/ShoppingList/CreateOrAddIngredients', { method: 'POST', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + 'RequestVerificationToken': token }, body: JSON.stringify({ recipeId: '@Model.Id', ingredientIds: selectedIngredientIds }) }); @@ -59,10 +64,11 @@ if (response.ok) { var result = await response.json(); localStorage.setItem('shoppingListId', result.shoppingListId); - alert('Shopping list updated.'); + alert('Zutaten wurden zur Einkaufsliste hinzugefügt.'); } else { - alert('Failed to update shopping list.'); + alert('Fehler beim Hinzufügen zur Einkaufsliste.'); } } + }