导出文件接口报300,前端解决方案

当后台返回的报错信息是一个流的时候,前端不能直接拿到信息,需要进行转化。

axios({
  url: '接口地址',
  method: 'post',
  responseType: 'blob',
  headers, // 请求头
  data // 导出数据传参
}).then((res) => {
  if(res){
  // 将流转换为json
    var reader = new FileReader();
    reader.onload = function(event){
      let message  = '';
      try {
        message  = JSON.parse(reader.result);
        if(message.hasOwnProperty('responseCode') && message.responseCode=='300'){
         // 给出提示语,进行拦截提示
          that.$notify({
            type: 'error',
            message: message.messageList[0].message,
            duration: 1500
          });
        }
      }catch (e) {
       // 如果是正常情况,返回的文件流就不会出现response,所以用try...catch进行处理一下
        const content = res;
        const dowLoadFileName = "导出的文件名";
        this.exportSearchList(dowLoadFileName, content);
      }
    };
    reader.readAsText(res);
  }
}).catch((error) => {
  throw error;
});

猜你喜欢

转载自blog.csdn.net/CuiCui_web/article/details/89394528
今日推荐