js fetch api

本文介绍了使用Fetch API和RxJS进行网络请求的方法,包括GET和POST请求的具体实现,并展示了如何利用RxJS处理并发请求。

MDN

fatch

返回的对象

type string
url string
redirected boolean
status number
ok boolean
statusText string
headers object
body object
bodyUsed boolean
clone function

arrayBuffer function
blob function
formData function
json function
text function

接口

  @Get('get')
  root(): Observable < any > {
    return of({ msg: 'get res ...'})
  }

  @Get('get1')
  root1(): Observable < any > {
    return of({
      msg: 'get1 res...'
    });
  }

  @Get('get2')
  root2(): Observable < any > {
    return of({
      msg: 'get2 res...'
    });
  }

  @Post('post')
  create(): Observable < any > {
    return of({
      msg: 'post res...'
    });
  }

get 和 post

        get() {
          let headers = new Headers();
          headers.append('Content-Type', 'application/json');

          let option = {
            method: 'GET',
            headers: headers,
            mode: 'cors',
            cache: 'default'
          };

          let request = new Request('/get', option);
          fetch(request).then(res => res.json()).then(res => console.log(res));
        

        post() {
          let headers = new Headers();
          headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');

          let option = {
            method: 'POST',
            headers: headers,
            mode: 'cors',
            cache: 'default'
          };

          let request = new Request('/post', option);
          fetch(request).then(res => res.json()).then(res => console.log(res));

        }

rxjs的ajax

            rxjs.ajax.ajax.get('/get',{'content-type': 'application/json'})
            .pipe(
              rxjs.operators.map(res => res.response)
            )
            .subscribe(
              v => { console.log(v) },
              e => { console.log(e) },
              () => { console.log('complete') }
            );

            rxjs.ajax.ajax.post('/post',{'content-type': 'application/x-www-form-urlencoded;charset=utf-8'})
            .pipe(
              rxjs.operators.map(res => res.response)
            )
            .subscribe(
              v => { console.log(v) },
              e => { console.log(e) },
              () => { console.log('complete') }
            );

rxjs 包装 fetch

          let get$ = rxjs.from(fetch('/get', {
            method: 'GET',
            headers: headers
          }))

          get$.pipe( rxjs.operators.mergeMap(res => rxjs.from(res.json())))
          .subscribe(res => console.log( res))

并发请求

          let get$ = rxjs.from(fetch('/get', {
            method: 'GET',
            headers: headers
          }))

          let get1$ = rxjs.from(fetch('/get1', {
            method: 'GET',
            headers: headers
          }))

          let get2$ = rxjs.from(fetch('/get2', {
            method: 'GET',
            headers: headers
          }))

          rxjs.merge(get$, get1$, get2$)
            .pipe(
              rxjs.operators.mergeMap(res => rxjs.from(res.json()))
            )
            .subscribe(res => console.log(res), err => console.error(err))

转载于:https://www.cnblogs.com/ajanuw/p/8279944.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值