1
votes

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

Postman:405 Error

It seems like POST is not allowed in Postman - ControlAltDel
The problem is not on the frontend/with the request - the error is coming from your server. You should look there, what kind of middleware are you using? - Adam
Your server does not accept POST requests to /. What sort of server application is it? - Phil
@Adam might be jumping the gun going straight to "what kind of middleware are you using". We don't even know what the server process is. Could be anything (Express, Spring, Flask, etc) but I'd put money down that OP is using something like VSCode Live Server - Phil
Those live server extensions are for static site development. They don't handle POST requests. You need to use a real server application (like Django) for that - Phil