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

239
Views
Fetch POST - send JSON to backend

Im stuck at something very trivial trying to build my frontend. My main.js looks like this :

const textfield = document.getElementById('inpTextField')
const btnSendPost = document.getElementById('btnSendPost')

btnSendPost.addEventListener('click', () => {
    sendData()
})

function sendData() {
    let txtValue = textfield.value
    console.log(txtValue)
    let sendingJSON = {
        "body" : `${txtValue}`
    }

    fetch("http://localhost:3000/post", {
        method: "post",
        body: JSON.stringify(sendingJSON),
        headers: { "Content-Type": "application.json"}
    })
    .then(res => res.json())
    .then(json => console.log(json))
    .catch(err => console.log(err))
}

but my backend (express 4.18) doesnt register POST request, and console gives back enigmatic :

TypeError: Failed to fetch
    at <anonymous>:1:876
    at sendData (main.js:17:5)
    at HTMLButtonElement.<anonymous> (main.js:7:5)

index.js (backend)

const express = require('express')
const app = express()
const port = 3000
const bp = require('body-parser')

let randomJson = {
    "apple" : "cider viegar"
}

app.use(bp.json())
app.use(bp.urlencoded(({ extended: true})))

app.get('/',(req,res) => {
    res.json(randomJson)
})

app.post('/post', (req, res) => {
    var body = req.body
    console.log(body)
    res.send("ok")
})

app.listen(port, () => {
    console.log(`Server running on ${port}`)
})

any ideas ?

about 4 years ago · Santiago Gelvez
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!