javascript - fetchAPI 的 Headers 对象始终为空
问题描述
我有这个请求函数,它是 fetch API 的包装器,用于向我的 API 发出请求。但是当我的前端应用程序发出请求时,标头对象始终为空。我究竟做错了什么 ?
export function request(method, url, payload) {
const body = JSON.stringify(payload);
const headers = new Headers();
headers.append('Content-Type', 'application/json');
const parameters = {
headers: headers,
method: method,
body: body,
cache: "default"
};
return Observable.create(observer => {
fetch(url, parameters)
.then(response => {
observer.next(response);
observer.complete();
})
.catch(error => {
observer.error(error);
});
});
}
解决方案
试着让它成为一个简单的对象:
const headers = {
"Content-Type": "application/json"
};
推荐阅读
- algorithm - 查找包含特定节点集合的最短“路径”
- python - Tkinter/Python:努力通过 Entry.bind() 方法传递参数
- python - 如何使用 xarray concat 函数将每日温度数据的平均值存储在循环内?
- react-native - React Native web - 几个问题
- sql - 如何在 BigQuery 中找到每条记录(由 ID 标识)的所有不同值?
- html - 为什么我的新第二页显示黑色,代码甚至不起作用
- android - 使用 PasswordTransformationMethod 在 kotlin 中将字符屏蔽为星号
- python - 从多列python查找
- r - 如何将数据从 R 数据框中的第 i 行第 2 列更新到第 j 列第 1 行?
- go - 如何使用 redisgo Api 设置带有 Expiry 的密钥