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

151
Views
Fetch token from api and Intercepting ajax call in jquery

I am trying to get token from api and wanted to send each ajax call in jquery.

function fetchToken(){
      // duplicate backend call by `delay` returns a promise
      return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve('test');
        }, 500);
    });
    }
function interceptAjaxCall(){
 var ajaxReference = $.ajax;
  $.ajax = function (options) {
      fetchToken()
      .then(function (tokenfrombackend) { 
          console.log(tokenfrombackend);
          var success = options.success;
          options.headers = { 'token':tokenfrombackend}; //this does not set token in each ajax call header!
          options.success = function (data, textStatus, jqXHR) {
          if (success)
              success(data, textStatus, jqXHR);
          };
         //return ajaxReference .apply($, arguments); /*returning here fails ajax call*/
      })
               
      return ajaxReference.apply($, arguments); 
    }

Intercepting each ajax is working but does not set token in each ajax header even after getting token value. Thanks in advance

about 4 years ago · Juan Pablo Isaza
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!