js Failed to execute 'fetch' on 'Window': Failed to read the 'headers' property from 'RequestInit'
时间: 2023-10-28 09:06:00 浏览: 50
这个错误是因为 `fetch` 函数的第二个参数 `RequestInit` 没有正确设置。通常情况下,`headers` 是 `RequestInit` 的一个属性,用于设置请求头。
请确保你的代码中正确设置了 `headers` 属性。以下是一个示例:
```javascript
var url = "https://example.com/api";
var headers = new Headers();
headers.append("Content-Type", "application/json");
fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify({ name: "John", email: "john@example.com" })
})
.then(response => response.text())
.then(responseText => {
console.log(responseText);
})
.catch(error => {
console.error("Fetch request error:", error);
});
```
在上面的示例中,我们创建了一个 `Headers` 对象,并使用 `append` 方法添加了一个 `Content-Type` 请求头。
然后,在 `fetch` 函数的第二个参数中,我们通过 `headers` 属性将请求头传递给请求。
最后,我们使用 `.then` 方法来处理响应,并将响应转换为文本格式进行输出。
请确保你的代码中的 `headers` 属性正确设置,并且与示例代码中的用法类似。这样就能够成功发送带有自定义请求头的 `fetch` 请求了。