# 处理 Fetch API 中的 CORS 问题

**URL:** https://meta.discourse.org/t/handling-cors-issues-in-fetch-api/275350
**Category:** Development
**Created:** [2023年八月16日 08:18 UTC](https://meta.discourse.org/t/handling-cors-issues-in-fetch-api/275350 "2023-08-16T08:18:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![vish234](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vish234](https://meta.discourse.org/u/vish234)
#### Post date: [2023年八月16日 08:18 UTC](https://meta.discourse.org/t/handling-cors-issues-in-fetch-api/275350/1 "2023-08-16T08:18:52Z")

</div>

我正在做一个 Web 开发项目，我正在使用 Fetch API 向不同域发出跨域请求。但是，我遇到了 CORS（跨域资源共享）问题，我的请求被阻止了。我尝试了几种在线找到的解决方案，但仍然遇到麻烦。这是我的代码的简化版本：

```plaintext
fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error('获取数据时出错:', error);
  });

```

我听说过在服务器端使用 CORS 标头来允许跨域请求，但我不知道如何实现它们。有人能指导我如何正确处理 CORS 问题吗？我该如何配置我的服务器以允许来自我的域的请求？我在服务器端使用 Express.js。任何帮助都将不胜感激！
