Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

256
Vistas
"Uncaught SyntaxError: Unexpected token l in JSON at position 0", but still works, why?

First time using websockets with Go and getting a weird error that doesn't break the program, and still continue as if it was not a problem. The client is a ReactJS single page application.

JS Client:

 const socket = new WebSocket("ws://localhost:5000/ws");
            setConnection(socket);

            socket.onmessage = (e) => {
                const message = JSON.parse(e.data)
                console.log("message:", message)
          
                switch (message.Command) {
                  case "loginResult":
                    if (message.Result) {
                      console.log("login worked");
                    }else{
                      console.log("login did not work");
                    }
                    break;
                }
              }

Snippet of Go it is getting JSON from:

result := ws.LoginResult{
        BaseMessage: ws.BaseMessage{
            Command: "loginResult",
        },
        Result: false,
    }

b, err := json.Marshal(result)

if err != nil {
    fmt.Println(err)
    return
}

if err = conn.WriteMessage(msgType, b); err != nil {
    return
}

And the output:

in here
WebsocketProvider.tsx:20 message: {Command: 'loginResult', Result: false}
WebsocketProvider.tsx:27 login did not work
VM3502:1 Uncaught SyntaxError: Unexpected token l in JSON at position 0
    at JSON.parse (<anonymous>)
    at WebSocket.socket.onmessage (WebsocketProvider.tsx:19)
socket.onmessage @ WebsocketProvider.tsx:19

Anyone have any idea why this is the case?

Solution was found:

  • It was to do with the migration of code and that a left over line was making a call that was ignored, hence where it made no difference to the execution. Nothing to do with the JSON format being difference since this was automatically generate by Go libraries.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

the function you have written for onmessage will be run every time a valid websocket message has been received.

According to the debug log you have posted the bit that worked was when you did receive valid JSON from the server and the function ran to completion as evidenced by this line:

WebsocketProvider.tsx:27 login did not work

Mark the line number.

After this you get:

VM3502:1 Uncaught SyntaxError: Unexpected token l in JSON at position 0
    at JSON.parse (<anonymous>)
    at WebSocket.socket.onmessage (WebsocketProvider.tsx:19)
socket.onmessage @ WebsocketProvider.tsx:19

mark the line number :19 This is the line with your JSON.parse.

My guess would be that this is a new invalid json message and it did indeed panic here and the rest of the function did not run - the bit that ran was a previous message with valid json.

As for why it failed - put in a console.log before your JSON.parse as suggested by @emptyhua to see what exactly you are receiving.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda