當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript ajax.ajax函數代碼示例

本文整理匯總了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;未經允許,請勿轉載。