我正在构建一个小的JavaScript应用程序,以便使用API列出来自ActiveCollab的任务,但我正在讨论CORS问题。
发生此问题是因为ActiveCollab API响应未在响应中包含Access-Control-Allow-Headers,请参阅https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowHeaderFromPreflight。
ActiveCollab开发人员是否愿意向API响应中添加必要的标头?
谢谢你,米格尔
发布于 2020-07-24 22:39:25
回答我自己的问题。
对于本地开发,我使用了CORS浏览器扩展(https://addons.mozilla.org/en-GB/firefox/addon/cors-everywhere)来解决CORS缺少标题的问题。
在生产中,我们通过nginx为应用程序提供服务,并设置一个代理传递来设置正确的头部。应用程序使用代理地址,而不是ActiveCollab应用程序接口地址。
在应用程序设置中:
VUE_APP_AC_API_URL = '<SERVER_URL>/ac-forwarder/<ACTIVECOLLAB_ACCOUNT>/api/v1'在nginx站点设置中:
location /ac-forwarder/ {
proxy_pass https://app.activecollab.com/;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '<SERVER_URL>';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-Angie-AuthApiToken';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'GET') {
#add_header 'Access-Control-Allow-Origin' '<SERVER_URL>';
add_header 'Access-Control-Allow-Methods' 'GET';
add_header 'Access-Control-Allow-Headers'
'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-Angie-AuthApiToken';
add_header 'Access-Control-Expose-Headers'
'Content-Length,Content-Range';
}
}
location /app {
alias /path/to/the/built/app;
}https://stackoverflow.com/questions/59160950
复制相似问题