本文整理汇总了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()
});
});
开发者ID:jameslnewell,项目名称:xhr-mock,代码行数:27,代码来源:rxjs.test.ts
示例2: switchMap
switchMap(() => ajax('/api/endpoint'))
开发者ID:cironunes,项目名称:angular,代码行数:1,代码来源:typeahead.ts
示例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
开发者ID:cironunes,项目名称:angular,代码行数:25,代码来源:error-handling.ts
示例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) {
// ...
}
开发者ID:cironunes,项目名称:angular,代码行数:25,代码来源:backoff.ts
示例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
开发者ID:cironunes,项目名称:angular,代码行数:28,代码来源:simple-creation.ts
注:本文中的rxjs/observable/dom/ajax.ajax函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论