美文网首页
Fetch请求如何拦截后面的then

Fetch请求如何拦截后面的then

作者: big_fang | 来源:发表于2018-12-28 18:58 被阅读0次

Q1:在报表导出中,使用了fetch,现在需要处理一个情况:接口会有返回404的状态。

  handleExport = () =>{
    if( this.state.type === ''){
        message.warning('请先选择区域');
    }else{
        getToken().then(token=>{
            const url = `${Url()}shopping_mall/reports/rank/export?from_date=${getStartDate(this.state.stDate)}&to_date=${getEndDate(this.state.endDate)}&order_by=${this.state.orderBy}&${this.state.type}`;
            const fetchOption = {
              method: 'GET',
              headers: {
                'X-Requested-With': '1000',
                Authorization: `Bearer ${token}`,
              },
            };
            // 开始所需数据的下载
            fetch(url, fetchOption)
              .then(response => response.blob())
              .then(blob=>{
                const aUrl = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = aUrl;
                document.body.appendChild(a);
                a.download= "排行数据详细表.csv";
                a.click();
                setTimeout(()=>{
                  document.body.removeChild(a);
                  window.URL.revokeObjectURL(aUrl);
                },2000);
              })
          })
    }
  }

修改后

  handleExport = () =>{
    if( this.state.type === ''){
        message.warning('请先选择区域');
    }else{
        getToken().then(token=>{
            const url = `${Url()}shopping_mall/reports/rank/export?from_date=${getStartDate(this.state.stDate)}&to_date=${getEndDate(this.state.endDate)}&order_by=${this.state.orderBy}&${this.state.type}`;
            const fetchOption = {
              method: 'GET',
              headers: {
                'X-Requested-With': '1000',
                Authorization: `Bearer ${token}`,
              },
            };
            return new Promise((resolve, reject) => {
              // 开始所需数据的下载
              fetch(url, fetchOption)
                .then(response => {
                  // console.log('response = ', response);
                  if(response.ok){
                    return response.blob();
                  }else{
                    message.warning('暂无数据导出');
                    throw `${response.statusText}`;
                    // throw new Error("error");
                    // message.error('')
                  }
                })
              .then(blob=>{
                const aUrl = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = aUrl;
                document.body.appendChild(a);
                a.download= "排行数据详细表.csv";
                a.click();
                setTimeout(()=>{
                  document.body.removeChild(a);
                  window.URL.revokeObjectURL(aUrl);
                },2000);
              })
              .catch(err => {
                reject(err);
              });
            });

          })
    }
  }

其中有个eslint提示语法报错:

expected an object to be thrown. (no-throw-literal)

因为语法要求写成这种:throw new Error(${response.statusText});,(语法规范:https://cn.eslint.org/docs/rules/no-throw-literal),于是这样改了,但是页面导出时,一旦是404的状态,就会跳转到报错页面,用户体验不友好,目前不知如何解决。先Mark

于是还是写成语法报错形式:throw${response.statusText};,这样不会跳转到报错界面。

参考来源:http://react-china.org/t/fetch-then/7054

相关文章

  • Fetch请求如何拦截后面的then

    Q1:在报表导出中,使用了fetch,现在需要处理一个情况:接口会有返回404的状态。 修改后 其中有个eslin...

  • fetch拦截器的实现

    fetch拦截器(interceptors)一般用于发起http请求之前或之后对请求进行统一的处理,如token实...

  • fetch记录一下子

    1.原生http请求 2.fetch请求 3.上面是封装多得fetch,直接使用的fetch fetch请求对某些...

  • 无代码侵入统计Alamofire所有请求

    我们在上一篇文章中如何拦截iOS所有网络请求介绍了如何使用NSURLProtocol拦截所有的网络请求添加统计代码...

  • WKWebView缓存+请求拦截(对比VasSonic)

    上一篇文章介绍了WKWebView如何网络请求拦截WKWebView完美网络请求拦截[https://www.ji...

  • dva 中使用 axios 统一拦截错误示例

    dva 官网使用的网络请求库是 dva/fetch,个人比较喜欢 axios,因为可以跨域,各种拦截使用起来也很舒...

  • ReactNative网络请求

    json字符串形式的fetch请求 form 形式的fetch请求

  • 如何终止 Fetch 请求

    到目前为止,在浏览器里用 JavaScript 发起请求的方式有两种:XMLHttpRequest 和 fetch...

  • 2018-06-02 axios入门

    1. interceptors(拦截) 在你发送请求后,直接拦截请求,然后返回指定的东西,这样不需要后台就可以完成...

  • InterceptorTest类: 这样,所有的 mvc:mapping里面的请求,都会被拦截。

网友评论

      本文标题:Fetch请求如何拦截后面的then

      本文链接:https://www.haomeiwen.com/subject/uisslqtx.html