在Aurelia中添加"访问控制允许来源"标头



当我尝试通过Aurelia前端访问我的API时,出现以下错误:

XMLHttpRequest 无法加载 https://[我的 AWS API URL 在这里]/auth。 对预检请求的响应未通过访问控制检查:否 "访问控制允许源"标头存在于请求的上 资源。因此不允许使用原产地"http://localhost:9000" 访问。响应具有 HTTP 状态代码 403

这是我的代码:

 constructor(Aurelia, HttpClient) {
    HttpClient.configure(http => {
      http.withBaseUrl(config.baseUrl);
    });
    this.http = HttpClient;
    this.app = Aurelia;
    this.session = JSON.parse(localStorage[config.tokenName] || null);
  }
login(username, password) {
    var client = new HttpClient();
    client.createRequest(config.apiUrl + '/auth', { username, password })
      .asPost()
      .withHeader('Access-Control-Allow-Origin', '*')
      .withBaseUrl(config.baseUrl)
      .send()
      .then((response) => response.content)
      .then((session) => {
        localStorage[config.tokenName] = JSON.stringify(session);
        this.session = session;
        this.app.setRoot('app');
    });
  }

我做错了什么?

它在邮递员中工作正常。

我通过使用 AWS API 网关界面中的"启用 CORS"并再次部署 API 来解决此问题。感谢您为我指出正确的方向!!

最新更新