Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
JavaScript post / fetch API and redirect

hello I ' m a beginner in node js i need someone to help me to fix my code. I want to make post request with fetch API then to redirect to the next page.

first I used the fetch to get the data the forms , send it to the data base and redirect then rediredt the user to the next page.

thank you for help!

***

<script>
  document.querySelector('#btnsinscr')
    .addEventListener('click', (e) => {
      e.preventDefault();
      var nom = document.querySelector('#nom').value;
      console.log(nom)
      var prenom = document.querySelector('#prenom').value;
      var email = document.querySelector('#email').value;
      var service = document.querySelector('#service').value;
      var fonction = document.querySelector('#fonction').value;
      var motdepasse = document.querySelector('#motdepasse').value;
   
        fetch('/data1', {
        method: 'POST',
        headers: {
          Authorization: 'Bearer abcdxyz',
          'Content-Type': 'application/json',
        },
        mode: 'cors',
        body: JSON.stringify({
          nom,
          prenom,
          email,
          motdepasse,
          fonction,
          service
        }),
        redirect: 'follow'

      })
      
        
      }



    );


   

</script>

route.post('/data1', (req, res,next) => {
    const { nom, prenom,email,motdepasse,fonction,service} = req.body;
    console.log(req.body)
    const { authorization } = req.headers;
  value= [[nom,prenom,email,motdepasse,fonction,service]]
    const sqlInsert = 'INSERT INTO people(nom,prenom,email,motdepass,fonction,service) VALUES ?'
    const insert_query = mysql.format(sqlInsert,[value])
    mysqlconnection.query (insert_query,function (error, results,fields){
        if(error) throw error;
      //console.log(results);

  });

    req.session.regenerate(function(err) {
    req.session.nom = nom;
    req.session.prenom = prenom;
    req.session.email = email;
    req.session.service = service;
    req.session.fonction = fonction;
    req.session.save()
    console.log(req.session)

    });

    res.redirect(307, '/Accueil');
    next()

    res.send({
    nom,
    prenom,
    email,
    motdepasse,
    fonction,
    service,
    authorization,
    });
    //res.render('./pages/accueil')

    
  })


about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!