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

302
Views
How to convert beforeSend of ajax itno axios

I'm switching from ajax to axios, this is the code I wanted to convert.

$.ajax({
            method: 'GET',
            url: SPSConstants.SPS_BASE_URL + 'api/test?filter=' + encodeURIComponent(JSON.stringify(filter)),
            dataType: 'json',
            beforeSend: function (xhr) {
                SPSUtils.getReqHeader(xhr, self, this, true);
            },
        }).done(function (result) {
            var resultObj = JSON.parse(JSON.stringify(result));
            let totalRecords = resultObj.pageInfo.total;
            let currentPage = self.state.currentPage;
            let currentRecords = resultObj.tollFreeNumberMasters;

            if (totalRecords !== 0 && currentRecords.length === 0) {
                
                self.setState({ currentPage: 1, pageInfo: { ...pageInfo, offset: 0 } }, () => {
                    self.loadTollFreeNumberTblData();
                });
            }
})

I've worked with axios earlier, so its not big of a deal. The only problem I'm facing is converting this part of ajax code into axios.

beforeSend: function (xhr) {
    SPSUtils.getReqHeader(xhr, self, this, true);
},

is there any beforesend equivalent in axios?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems that you want to get the xhr object before the http request. It is the Interceptors that functions similarly in the Axios.

axios.interceptors.request.use(config => {
  console.log(config);
});

However, if you set the interceptors function on an Axios instance, the callback will work for all requests from that instance. To make callback work only for specific requests, you need to create and use a new instance of Axios.

const newAxios = axios.create();
newAxios.interceptors.request.use(config => {
  console.log(config);
});
about 4 years ago · Juan Pablo Isaza 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!