没有'Access-Control-Allow-Origin'标题错误 - 但它在那里

时间:2018-02-08 10:42:50

标签: javascript reactjs

您好我已经环顾四周并尝试了一些事情,无法让它发挥作用。

我有这个获取请求:

  index = () => {
    return fetch(`${baseUrl}api/deliveries`, {
      method: 'GET',
      headers: new Headers({
        'Access-Control-Allow-Origin': 'http://localhost:3000',
        'Content-Type': 'application/json',
      }),
    })
      .then(res => res.json());
  }

我在控制台中收到此错误:

Failed to load http://localhost:8000/api/deliveries: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

错误状态'Access-Control-Allow-Origin'不存在。但它是。它在那里。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您不需要发送此标头,它需要出现在服务器的响应中。

答案 1 :(得分:0)

它应该在服务器上的OPTIONS请求的响应中,正如错误消息所示。