question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ajax config & intercept

See original GitHub issue

Is there a way to create a pre configured ajax instance, so that I don’t need to configure it over and over again ?

For example:


import ajax from 'rxjs/Observable/dom/ajax'

const myAjax = ajax.create({url: '/api/xxx',  header: {widthCredential: true, ...}})

// Now myAjax will setup widthCredential = true for all the related outgoing ajax request
// so that  I can just:
myAjax.get('/api/xxx')
myAjax.post('/api/xxx', body)

// Instead of
ajax.get({url: '/api/xxx', header: {widthCredential: true, ...}})
ajax.post({url: '/api/xxx', body: data, header: {widthCredential: true, ...}})

BTW how can I intercept all http requests or responses in a single place using rxjs’ ajax api ?

Here’s how the axios library intercepts http requests and responses:


// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

Thanks !

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:33
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
benleshcommented, Jan 12, 2022

Core Team: General lack of interest in this feature as of right now.

@benlesh: I’ll think about the problem though, since it seems like something a lot of people are asking for.

1reaction
gugadevcommented, May 5, 2020

You can still use Axios with interceptor and use fromPromise to create an Observable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Intercept all ajax calls? - Stack Overflow
If you're using jQuery, $.ajaxSuccess is a good option, but here's a generic option that will intercept XHR calls from all frameworks (I've ......
Read more >
How to intercept ajax calls in jQuery? - My Programming Notes
This post is good tutorial about ajax interception technique. Basically, it introduces two ajax interceptors, one is javascript interceptor, the ...
Read more >
Intercept HTTP requests - AJAX/jQuery - Diwebsity
Working in JavaScript we often need to make some HTTP requests to server. Requests could pass some information or try to get some...
Read more >
jQuery ajax request interceptor - CodeSandbox
ajaxSetup ({. beforeSend: function(xhr, setting) {. console.log("intercept before ajax send");. console.log(xhr, setting);. }.
Read more >
Intercept Service - WebdriverIO
With wdio-intercept-service you can intercept ajax HTTP calls initiated by some ... as it requires some setup work that can only be done...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found