I'm facing an issue with CSRF verification in a Django project when making an AJAX POST request. Here's a simplified version of my code:
**registration.html ***
<form method="POST" onsubmit="return validateForm()">(----this revieves the data------) {% csrf_token %}<label for="name">Name:</label><input type="text" id="name" name="name" required /><label for="email">Email:</label><input type="email" id="email" name="email" required /><label for="password">Password:</label><input type="password" id="password" name="password" required /><input type="submit" value="Register" name="createuser"/></form></div></div><script> let URLd ="{% url 'defaultpg' %}" let nameInput = document.getElementById("name"); let emailInput = document.getElementById("email"); let passwordInput = document.getElementById("password");(------below funtion validates it -----) function validateForm() { var csrfToken = $("input[name='csrfmiddlewaretoken']"); let nameValue = nameInput.value; let emailValue = emailInput.value; let passwordValue = passwordInput.value; let isNameValid = /^[a-zA-Z]+$/.test(nameValue); let isEmailValid = /^\S+@\S+\.\S+$/.test(emailValue); let isPasswordValid = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/.test(passwordValue); if (isNameValid) { if (isEmailValid) { if (isPasswordValid) { alert("Successful"); $.ajax({ type: "POST", url: '/defaultpg', headers: {"X-CSRFToken":'{{ csrf_token }}'}, data: {"name": nameValue,"email": emailValue,"password": passwordValue,'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]") }, dataType: "json", success: function (data) { // any process in data alert("successful"); }, error: function () { alert("failure"); } }); } else { alert("Password must contain letters, capital letter, small letter, special character, and numbers with a length above 8"); } } else { alert("Please enter a valid Email Address"); } } else { alert("Please enter a valid Name"); } }</script>view.py@csrf_protectdef defaultpg(request): if request.method == "POST": name = request.POST.get('name') email = request.POST.get('email') password = request.POST.get('password') print("------------------------") print(name) print(email) print(password) print("------------------------")#I put print statement just to see whether data is received or not return redirect('Entry')urls .py path('eafterhome/', views.defaultpg, name='defaultpg'),I just want that the data shall be received by view.defaultpg ,Im new to django i m doing this for my final yr project a little help would be nice