本文整理汇总了TypeScript中rxjs/observable/dom/ajax.ajax函数的典型用法代码示例。如果您正苦于以下问题:TypeScript ajax函数的具体用法?TypeScript ajax怎么用?TypeScript ajax使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ajax函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should return a JSON object', done => {
mock.post('/some-url', {
body: JSON.stringify({data: 'mockdata'})
});
ajax({
url: '/some-url',
body: {some: 'something'},
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
responseType: 'json'
}).subscribe({
next: response => {
try {
expect(response.response).to.be.deep.equal({
data: 'mockdata'
});
} catch (error) {
done(error);
}
},
error: error => done(error),
complete: () => done()
});
});
示例2: switchMap
switchMap(() => ajax('/api/endpoint'))
示例3: ajax
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
// #docregion
import { ajax } from 'rxjs/observable/dom/ajax';
import { map, catchError } from 'rxjs/operators';
// Return "response" from the API. If an error happens,
// return an empty array.
const apiData = ajax('/api/data').pipe(
map(res => {
if (!res.response) {
throw new Error('Value expected!');
}
return res.response;
}),
catchError(err => Observable.of([]))
);
apiData.subscribe({
next(x) { console.log('data: ', x); },
error(err) { console.log('errors already caught... will not run'); }
});
// #enddocregion
示例4: backoff
import { ajax } from 'rxjs/observable/dom/ajax';
import { range } from 'rxjs/observable/range';
import { timer } from 'rxjs/observable/timer';
import { pipe } from 'rxjs/util/pipe';
import { retryWhen, zip, map, mergeMap } from 'rxjs/operators';
function backoff(maxTries, ms) {
return pipe(
retryWhen(attempts => range(1, maxTries)
.pipe(
zip(attempts, (i) => i),
map(i => i * i),
mergeMap(i => timer(i * ms))
)
)
);
}
ajax('/api/endpoint')
.pipe(backoff(3, 250))
.subscribe(data => handleData(data));
function handleData(data) {
// ...
}
示例5: fromEvent
// Create an Observable that will publish mouse movements
const mouseMoves = fromEvent(el, 'mousemove');
// Subscribe to start listening for mouse-move events
const subscription = mouseMoves.subscribe((evt: MouseEvent) => {
// Log coords of mouse movements
console.log(`Coords: ${evt.clientX} X ${evt.clientY}`);
// When the mouse is over the upper-left of the screen,
// unsubscribe to stop listening for mouse movements
if (evt.clientX < 40 && evt.clientY < 40) {
subscription.unsubscribe();
}
});
// #enddocregion event
// #docregion ajax
import { ajax } from 'rxjs/observable/dom/ajax';
// Create an Observable that will create an AJAX request
const apiData = ajax('/api/data');
// Subscribe to create the request
apiData.subscribe(res => console.log(res.status, res.response));
// #enddocregion ajax