为什么我的 JavaScript 代码会收到“No 'Access-Control-Allow-Origin'”错误?

问题描述

我正在尝试在 react 组件中添加 API 端点,以使用 Bold Commerce 中已有的代码获取数据。

var data = null;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange",function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("GET","https://api.boldcommerce.com/subscriptions/v1/shops/:shop_identifier/subscription_groups");
xhr.setRequestHeader("accept","application/json");
xhr.setRequestHeader("authorization","Bearer {Access Token}");

xhr.send(data);

代码报错

Access to XMLHttpRequest at 'https://api.boldcommerce.com/subscriptions/v1/shops/:shop_identifier/subscription_groups' from origin 'http://localhost:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我尝试了很多方法,但都没有找到合适的解决方

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)