JS axios 从 blob response type 获取 json 数据 发表于 2023-02-13 更新于 2023-03-21 分类于 JavaScript 本文字数: 968 阅读时长 ≈ 1 分钟 用 Axios 上传文件的几种方式 12345678910post("some/api/url", someDataForBackend, { responseType: "blob", }) .then(async (response) => { const isJsonBlob = (data) => data instanceof Blob && data.type === "application/json"; const responseData = isJsonBlob(response?.data) ? await (response?.data)?.text() : response?.data || {}; const responseJson = (typeof responseData === "string") ? JSON.parse(responseData) : responseData; console.log(responseJson) }); 12345678910111213141516axios.get(`/download/blob/api`, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', }, validateStatus: (s) => s <= 500, responseType: 'blob',}).then(async (res) => { if (res.status !== 200) { // error handling const error = JSON.parse(await res.data.text()); console.log('error: ', error); } else { // success blob file }}); 后记 https://github.com/axios/axios/issues/3779 相关文章 JS axios 上传文件 JS axios 使用 eventstream 解决Axios额外的发起一次option请求 JS调试 - 跳过或者忽略不需要的js文件 - Blackbox Chrome Extension Scripts 使用 Docker 构建前端项目 打赏 微信支付 支付宝 本文作者: Mt.r 本文链接: https://trycoding.fun/JavaScript/js-axios-get-get-json-response-from-blob-response-type/ 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!