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

297
Views
Solicitud POST de JavaScript al error del servidor Golang: final inesperado de la entrada JSON

Estoy haciendo una solicitud POST en JavaScript a mi servidor Go para enviar algunos datos JSON. El backend recibe los datos con éxito.
Si registro en la consola el resultado de la promesa, obtengo un error de entrada inesperado al final de JSON en la línea donde se llama a fetchResponse.json() . No puedo entender por qué está sucediendo.

JavaScript

 const sendScore = async () => { try { const fetchResponse = await fetch(`http://localhost:8000/sendscore`, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({name:"kris"}) }); const data = await fetchResponse.json(); return data; } catch (e) { return e; } } const sendScoreBtn = document.querySelector("#sendScoreBtn"); sendScoreBtn.addEventListener("click", () => { console.log("Sending score...") const res = sendScore() res.then(console.log) })

Controlador de punto final Go /sendscore

 func recieveScore(w http.ResponseWriter, r *http.Request) { var res playerScore if err := json.NewDecoder(r.Body).Decode(&res); err != nil { fmt.Println("Line 31", err) } fmt.Println("Res", res) } func main() { // Serve static files(js, css) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) http.HandleFunc("/", homePage) http.HandleFunc("/sendscore", recieveScore) fmt.Println("Server started. Listening on port :8000") log.Fatal(http.ListenAndServe(":8000", nil)) }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Mi problema era no enviar la respuesta al navegador y, por lo tanto, no podía analizar ningún JSON. Se actualizó el controlador de Go, lo que hizo que el proceso funcionara.

 func recieveScore(w http.ResponseWriter, r *http.Request) { var res playerScore if err := json.NewDecoder(r.Body).Decode(&res); err != nil { fmt.Println("Line 31", err) } fmt.Println("Res", res) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(res) // Writing the result to response }
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!