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

333
Vistas
How can I handle error with fetch for 204 No content?

I have a fetch request as below;

fetch(url, {
    mode: 'no-cors',
    credentials: 'include'
}).then((response) => {
    if (response.ok) {
        return;
    }
    console.log(`Something wrong`);
})
.catch((error) => {
    console.log(`catch block error`);
});

My 204 response looks as

body: null
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""

My question is how do I handle this since my 204 response is kind of successful, but only thing is the ok flag is false So it goes and prints "Something wrong" which is not correct for 204 response.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You said mode: 'no-cors' so you can't look at the response. no-cors mode is only useful if:

  • you need to send a request
  • the request doesn't need any features which would make it preflighted
  • you don't need to know anything about the response

You can't read the status (this is why it is reported as 0 in the response you quoted), so you can't tell if it was 204 No Content or 200 OK or 500 Internal Server Error, etc.

You can't read the body, so you can't tell if there was no content by testing it.


You need to:

  • Remove mode: 'no-cors'
  • Make sure the server grants all the permissions you need using CORS
  • Read the status property of the response object to determine if it is a 204 or not
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