I am trying to get a user’s email and save it to a database without refreshing the page using Axios but, I keep getting a 405 error. Also, I am trying to connect to my localhost using post from postman api and I am getting a 405 error as well.
Html:
<form id="emaildata" action="" method="POST" role="form">
<input type="email" name="email" id="joinme" placeholder="Email:">
<input type="submit" value="Subscribe">
</form>
JS/Axios:
(function() {
"use strict";
document.getElementById("emaildata").addEventListener("submit", function(e) {
e.preventDefault();
let email = document.getElementById("joinme").value;
const emailData = new FormData();
emailData.append('email', email);
emailData.append('csrfmiddlewaretoken', '{{ csrf_token }}');
axios.post('http://127.0.0.1:8000', emailData) // 4
.then(res => alert("Form Submitted")) // 5
.catch(errors => console.log(errors)) // 6
});
})()