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

308
Vistas
Angular 2/Web Api - json parsing error syntax error unexpected end of input

I have a Web API controller that looks like this:

    [HttpPost]
    public IHttpActionResult Test()
    {
        return Ok();
    }

This is correct syntax, However when I try to call this from a service in Angular 2, I get the error message: "json parsing error syntax error unexpected end of input." To resolve this issue, I have to put a value into the ActionResult such as

return Ok(1)

Am I missing some configuration? My Angular 2 service call looks like this:

return this.http.post(API/Controller/Test).map(res => res.json());
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Another way:

All my http.get, post or put call (for uniformity):

.map(this.extractData)

In extractData (UPDATED: thanks to raykrow feedback, short code):

private extractData(res: Response) {        
    return res.text() ? res.json() : {}; ;
}
over 4 years ago · Santiago Trujillo Denunciar

0

I guess that when you receive an empty response (without payload) you don't need to call the json method. Under the hood, the XHR response is undefined, JSON.parse(undefined) is called and an error is thrown.

You could skip the call of the map operator:

return this.http.post(API/Controller/Test)/*.map(res => res.json())*/;
over 4 years ago · Santiago Trujillo Denunciar

0

Strictly speaking if your API is returning an OK code with no content you should return 204 (no content) - Angular would also be happy with this and not try to parse anything. So your controller method would become:

[HttpPost]
public IHttpActionResult Test()
{
    return NoContent();
}
over 4 years ago · Santiago Trujillo 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