Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
405 Error on Fetch to send Data from front to back

I am working on a registration form in HTML. The form uses script to take the data entered by user, and turn it into JSON format so that my backend can process it. My text editor is Visual Studio Code and I am checking the functionality of my code with Five Server extension. When I run the code, I get a 405 (method not allowed) message whenever I try to submit data. I suspect that when I make the fetch call, I am not properly referencing the resource. Does my JavaScript file need to be in the same folder as my HTML file? Or is my issue something else?

 <script>
        const form = document.getElementById('createAccount')
        form.addEventListener('submit', registerUser)
        // Will send Data as JSON
        async function registerUser(event) {
            event.preventDefault()
            const firstName = document.getElementById('signupFname').value
            const lastName  = document.getElementById('signupLname').value
            const email     = document.getElementById('signupEmail').value
            const username  = document.getElementById('signupUsername').value
            const password  = document.getElementById('signupPword').value
            
            //Is this right?
            const result = await fetch('/signup', {
                method: 'POST',
                headers: {
                    'Content-Type':'application/json'
                },
                body: JSON.stringify({
                    firstName,
                    lastName,
                    email,
                    password,
                    username

                })
            }).then((res) => res.json())

            console.log(result)
        }
    </script>

Here is my Javascript

const express = require("express");
const router = express.Router();
const path = require('path');
router.use('/', express.static(path.join(__dirname, 'static')));

// mongodb user model
const User = require("./../models/users");

// Password handler
const bcrypt = require("bcryptjs");

// Signup
router.post("/signup", (req, res) => {
  let { fname, lname, username, password, email } = req.body;
  fname    = fname.trim();
  lname    = lname.trim();
  username = username.trim();
  password = password.trim();
  email    = email.trim();

  if()
// Trimmed code for sake of space
)};
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda