Fetch适配器
fetch 适配器是 alova 的预定义的请求适配器,通过它可以使用 fetch API 的所有功能。
使用方法
设置请求适配器为 fetch 适配器
import { createAlova } from 'alova';
import adapterFetch from 'alova/fetch';
const alovaInst = createAlova({
requestAdapter: adapterFetch()
// ...
});
配置项
创建 method 实例时,可传入任何fetch API支持的参数,这些参数会在请求时传给fetch函数。
alovaInstance.Get('/todo/list', {
// ...
credentials: 'same-origin',
referrerPolicy: 'no-referrer',
mode: 'cors'
});
以上 method 实例在通过fetch发送请求时,将会以以下参数请求。
fetch('/todo/list', {
// ...
credentials: 'same-origin',
referrerPolicy: 'no-referrer',
mode: 'cors'
});
fetch API的请求体支持传递string | FormData | Blob | ArrayBuffer | URLSearchParams | ReadableStream参数。
Content-Type 处理
fetch 适配器在未指定Content-Type请求头,并且请求体数据不是FormData时,默认设置Content-Type为application/json;charset=UTF-8。你可以设置Content-Type请求头来覆盖默认行为,例如:
alovaInstance.Post(
'/todo/create',
{ title: 'New Todo' },
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
[v3.4.0+]如果不希望设置默认的Content-Type,可以将Content-Type设置为falsy值,即undefined、null、false或空字符串。
alovaInstance.Post(
'/todo/create',
{ title: 'New Todo' },
{
headers: {
'Content-Type': undefined
}
}
);
alova使用调查,花3分钟填下,求你了!
alova用在项目里了吗?快来告诉我