Ajax config & intercept
See original GitHub issueIs 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:
- Created 6 years ago
- Reactions:33
- Comments:17 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
You can still use Axios with interceptor and use
fromPromise
to create an Observable.