【发布时间】: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