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

635
Views
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 answers
Answer question

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 Report

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 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!