【问题标题】:CORS in Ionic and third party api(Smartsheet)Ionic 中的 CORS 和第三方 api(Smartsheet)
【发布时间】:2018-06-29 14:10:05
【问题描述】:

我正在尝试从 smartsheet API 获取数据。当我在单独的服务器代码中使用 postman 和 nodejs 时,它可以工作。

但是,如果我在 Ionic 中使用带有 HttpClient (@angular/http) 的 API,则会在浏览器中运行时出现 CORS 问题。

Failed to load https://api.smartsheet.com/2.0/sheets/1235941564208899972: 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:8100' is therefore not allowed access.

我也尝试过如下代理设置:

  "proxies": [{
    "path": "/2.0",
    "proxyUrl": "https://api.smartsheet.com/"
  }]

home.ts:

let headers = new Headers({ 'Access-Control-Allow-Origin': '*', 'Authorization': 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxx' });
let options = new RequestOptions({ headers: headers });

this.HttpClient.get('/2.0/sheets/1235941564208899972', options).pipe(
    map(res => res.json())
).subscribe(data => this.response = data);

我仍然只收到 localhost 404 错误。

【问题讨论】:

  • 你可以为此使用 cors chrome 扩展
  • 如果我使用 cors 扩展程序,它将在该特定浏览器中工作,在构建时它将无法工作@PareshGami
  • 当您在真实设备中构建和运行时,不会出现 cors 错误
  • 如果我理解正确,获取构建应用程序后不会出现交叉问题。我可以吗? @PareshGami,..
  • 是的,你理解正确

标签: node.js ionic-framework npm


【解决方案1】:

如果 Ionic 正在使用 CORS 与 Smartsheet API 对话,那将无法正常工作。 Smartsheet API 目前不支持 CORS。

【讨论】:

  • 您将通过 Node 等服务器端技术与 Smartsheet API 进行交互。
  • 是的,使用单独的服务器端代码完成。但我试图在没有服务器端的情况下进行修复,只使用 api http 客户端请求来修复,.. 这将感觉代码减少作为我的假设..(在第一行查询中,我早先提到了服务器端)
猜你喜欢
  • 2014-11-09
  • 2020-11-14
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2017-10-13
相关资源
最近更新 更多