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

173
Views
usar fetch con express y ejs da 400 solicitudes incorrectas, así como un token inesperado < en json

Al usar fetch, recibo una solicitud incorrecta, así como un token inesperado < en JSON, ambos apuntan a search.js, que es donde realizo la solicitud de fetch que intenté enviar a través de un cliente API y está funcionando. envíe la solicitud de recuperación a través de input onkeyup. También noté que mi error no se detecta

Solicitud de obtención:

 const searchResults = function(e){ fetch('/elpers/search' , { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(e.value) }).then(res => { res.json() }).then(data => { console.log(data) }).catch(err => { console.error(err) }) }

ruta POST

 const postSearch = (req, res) => { console.log(req.body) res.send({'result': 'value'}); }

editar: esto funcionó para mí JSON.stringify({result: e.value})

Error:

search.js:2 POST http://localhost:3000/elpers/search 400 (Bad Request) VM118:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe usar res.json() en lugar de res.send() y JSON.stringify(e.value) a JSON.stringify({result: e.value})

about 4 years ago · Juan Pablo Isaza Report

0

usa esto y déjame saber:

 const postSearch = (req, res) => { console.log(req.body) // post if data is received here... res.json({ message: "data received!", data: search, // search should be the data that user fetched }) };

En el lado del cliente, solo consola.Registre los datos recibidos y vea si es JSON o no :)))

¡Feliz codificación!

about 4 years ago · Juan Pablo Isaza Report
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!