Getting 405 Method Not Allowed on a request

System Information
  • Strapi Version: 4.0.2
  • Operating System: Windows
  • Database: MySQL
  • Node Version: 14.18.0
  • NPM Version: 6.14.15
  • Yarn Version: n/a

Hey all, I’m getting 405 Method Not Allowed error on my request.

This is what my fetch function looks like:

function submitForm(e, form) {
    e.preventDefault();

    fetch('http://localhost:1337/train-with-us', {
        method: 'post',
        body: JSON.stringify({
            name: form.name.value,
            dob: form.birthday.value,
            email: form.email.value,
        }),
        headers: new Headers({'content-type': 'application/json'}),
    })
        .then(function (response) {
            return response.json();
        })
        .then(function (data) {
            document.getElementById('train_form_message').innerHTML = 'Your subscription has been successfully submitted.';
        })
        .catch(function (err) {
            document.getElementById('train_form_message').innerHTML = 'Sorry, an error occurred.';
        });
}

And this is the HTML:

            <form onsubmit="submitForm(event, this)">
                <div class="input-wrap">
                    <label for="name">Name</label>
                    <input type="text" name="name" id="name" required />
                </div>
                <div class="input-wrap">
                    <label for="birthday">Date of Birth</label>
                    <input type="text" name="birthday" id="birthday" required />
                </div>
                <div class="input-wrap">
                    <label for="email">E-mail</label>
                    <input type="email" name="email" id="email" required />
                </div>
                <button type="submit" class="btn btn-red">Register</button>
                <p id="train_form_message" class="train_form_message"></p>
            </form>

And this one’s the structure in the admin of Strapi:

On strapi v4 the API url has changed, it would be something like:
http://localhost:1337/api/train-with-us’, note that the world ‘api’ is missing on your request.

1 Like

Thank you!