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

632
Vistas
Using rxjs/ajax interceptor in React

I use rxjs/ajax in React so I need to add an interceptor to handle header and response (global errors), is there any suggestion?

Thank you

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can create a higher order function to produce an ajax function with intercept feature and export this instance for later use.

const withIntercept=(preInterceptor, postInterceptor) => {

return (param) =>ajax(preInterceptor(param)).pipe(
  map(postInterceptor),
  catchError(error => {
    console.log('error: ', error);
    return of(error);
  })
);
}

// usage 
export const ajaxWithInterceptor=withIntercept=(
   param =>{ param.header={..new header }; return param }, 
   res => do something with response...)

   
ajaxWithInterceptor({url:xxx,header:xxx,body:xxxx,method:"post" }).subscribe()
over 4 years ago · Santiago Trujillo Denunciar

0

This is a sample for get request that handle errors, headers and base url for other methods we can copy and paste.

const baseURL = "http://example.com/api/";
export const getAjax = (endpoint, headers = null) => {
    return ajax.get(baseURL + endpoint, headers).pipe(
        catchError(error => {
            if (error.status === 404) {
                return of(error);
            } else {
                return new Promise((resolve, reject) => {
                    reject(error);
                });

            }

        })
    );
}
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