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