axios 的 responseType不是期望后端给你返回的类型,而是设置axios怎么解析response.data的值,默认是json,也可以改成text ,他就变成了一个字符串,或者Blob 他就变成了blob。 根本就不是网上中文文档胡说八道什么期望后端返回的数据类型。 响应头里的Content-Type后端返回的是类型,请求头里的Accept表示希望接受哪些类型。
firstName: 'Fred' }, // timeout:请求超时的毫秒数(0 表示无超时时间),超过将被中断 timeout: 1000, // withCredentials:跨域请求是否使用凭证,默认false不需要 withCredentials: false, // adapter:允许自定义处理请求,以使测试更轻松 // 返回一个 promise 并应用一个有效的响应 (查阅 [response docs](#re...
// 如果设置 responseType 为 'blob'axios.get('/image',{responseType:'blob'// 设置响应类型为 Blob}).then(response=>{consturl=window.URL.createObjectURL(newBlob([response.data]));constlink=document.createElement('a');link.href=url;link.setAttribute('download','file.jpg');// 设置下载文件名doc...
文件预览或下载中,axios设置responseType:blob时对于后台报错信息的捕获兼容,如果response.data.type是application/octet-stream,处理流数据及进行下载,如果是application/json,把返回的数据转化为JSON格式后获取message,code等,可以使用try catch,在try里面正常拿到数据进行下载处理,而在catch中先对数据流进行转化,然后再处理...
responseType和ContentType在使用axios时的区别如下:responseType:用途:设置axios如何解析response.data的值。默认值:json,即axios将尝试将响应数据解析为JSON对象。其他可选值:text:response.data将被解析为一个字符串。blob:response.data将变为一个Blob对象。关键点:responseType的设置与后端返回的数据...
在使用axios时,许多人对`responseType`和`Content-Type`这两个概念容易混淆。实际上,它们分别用于定义axios如何处理响应数据以及后端返回的数据类型。`responseType`的用途是设置axios如何解析`response.data`的值。默认设置为`json`,这意味着axios将尝试将响应数据解析为JSON对象。若将`responseType`设置为`...
const res = await axios.get('https://httpbin.org/get')res.data; // Returns the HTTP response body at the server we requested.typeof res.data; // object res.data 返回以下内容:{ args: {}, headers: { Accept: 'application/json, text/plain, _/_', Host: 'httpbin.org', ...
第一步:让后端将下载的接口的response header设置: Content-disposition: attachment; filename=数据报表.xlsx(表示会直接下载文件,文件名为‘数据报表’) Content-Type:application/octet-stream (二进制流数据,如常见的文件下载) 第二步:修改axios请求的responseType为blob,以get请求为例: ...
=="text"?request.response:request.responseText;constresponse={data:responseData,status:request.status,statusText:request.statusText,headers:responseHeaders,config,request,};resolve(response);}; 首先onreadystatechange是XMLHttpRequest实例的一个方法,可以监听响应事件,readyState也是XMLHttpRequest实例上的一个属性,...
resolve(response.data) }) .catch(err => { console.log(err) reject(err.data) }) }) 打印出的response如下: config: {url: 'https://www.iic.top/api/login/login', method: 'post', data: '{"req":{"account":"userName123","password":"123456","captcha":"jms7"}}', headers: {…},...