- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 getresponse.com 创建前端应用程序。它有自己的API,位于 https://api.getresponse.com 。当我尝试使用 Axios 或 Fetch 执行任何 javascript 请求表单浏览器时,出现此错误:
Access to XMLHttpRequest at 'https://api.getresponse.com/v3/accounts' from origin 'http://localhost:8080' 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.
在网络选项卡中我有:
Request URL: https://api.getresponse.com/v3/accounts
Request Method: OPTIONS
Status Code: 400 Bad Request
因此,据我所知,API 无法正确处理来自网络浏览器的“复杂”请求。
现在我只有一个想法 - 制作一些中间件,它将我的请求代理到 API 端点,而无需任何预检请求。
我说得对吗?我找不到此类中间件的任何示例。我在哪里可以找到任何信息?
最佳答案
A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood and a server is aware using specific methods and headers.
有关预检请求的更多详细信息,请参阅 here.
您可能应该研究一下如何处理 Content Security Policy在您的服务器上。您从 JavaScript 发出的所有请求都将由浏览器验证。因此,您的服务器应使用 header Access-Control-Allow-Origin
进行响应,了解有关 header here 的更多信息。
通过从浏览器网络选项卡进行检查,您应该能够查看响应中存在的 header 。
由于您无法控制 API
服务器,因此您可以创建 proxy server与外部服务器进行通信。详细可以找tutorial here .
这是一个 nginx 虚拟主机配置示例,它处理使用 /api
到 https://api.example.com
的所有请求的代理
server {
listen 80;
server_name example.com; # Your server domain, in your case localhost
location /api {
access_log off;
proxy_pass https://api.example.com; # <--- this will the target domain
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
因此,如果您创建http://example.com/api/v3/accounts
,这将被转发到https://api.example.com/api/v3/帐户
。
基本上,您的响应中应该包含以下 header 。
Access-Control-Allow-Origin: https://yourdomain.com
您还可以进行以下配置。
Access-Control-Allow-Origin: *
如果您使用withCredentials那么您不能在 Access-Control-Allow-Origin
header 中使用通配符 *
,您应该明确提及该域。
关于javascript - 外部 API 未处理 CORS 预检请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58077982/
我尝试访问此 API click here for more info POST https://api.line.me/v2/oauth/accessToken 但总是得到错误: XMLHttpRe
我在掌握 CORS 概念时遇到问题... 我认为同源策略保护应用程序不会对“不受信任的域”进行 ajax 调用。所以, mydomain.com 向 发出 ajax 调用somedomain.com
一个技术性很强的问题,可能只有了解浏览器内部结构的人才能回答...... 浏览器缓存 CORS 预检响应的准确程度如何(假设在对 OPTIONS 预检请求的响应中返回了 Access-Control-
我一直在阅读 CORS以及它是如何工作的,但我发现很多事情令人困惑。例如,有很多关于事情的细节,比如 User Joe is using browser BrowserX to get data fr
在 OWASP site 上看到这个矛盾,我感到很困惑。 CORS 备忘单: 使用 Access-Control-Allow-Credentials: true 响应 header 时要特别小心。将允
我们无法在飞行前恢复使用 cors:任何帮助都非常感谢 ==========错误========================== About to connect() to localhost p
跨域请求字体文件时,您必须确保允许请求域使用 CORS header 访问字体文件: 访问控制允许来源 访问控制允许凭据 然而,这在请求图像时不是必需的,无论是对于 img元素或 background
CORS 规范没有说明服务器应如何响应无效的 CORS 请求。例如,如果请求 Origin 无效,则 CORS spec states :“终止这组步骤。请求超出了本规范的范围。”其他错误情况也有类似
我在理解同源策略和“解决”它的不同方法时遇到了一些麻烦。 很明显,同源策略是作为一种安全措施而存在的,因此来自服务器/域的一个脚本无法访问来自另一个服务器/域的数据。 也很明显,有时能够打破此规则很有
我正在尝试使用 cloudformation 在 API Gateway 中部署 API。这些方法需要启用 CORS,我遵循此处的模板 Enable CORS for API Gateway in C
我正在构建一个使用 CORS 的 REST 应用程序。每个 REST 调用都是不同的,我发现获取预检 OPTIONS 调用会产生很大的开销。有没有办法缓存并应用预检选项结果,以便对同一域的任何后续调用
我正在将我的 WebApi 升级到 MVC6。 在 WebApi 中,我可以拦截每个 HTTP 请求,如果是预检,我可以用浏览器可以接受的 header 进行响应。 我正在尝试找出如何在 MVC6 W
假设一个 CORS 预检请求进来了,但它为一个或多个 Access-Control-Request-* 指定了一个不受支持的值。标题。服务器应该如何将其传达回浏览器? 一些例子: 浏览器发送带有 Ac
问题中的一切。 附加信息: 使用 Win 10,GraphDB 免费,9.1.1 • RDF4J 3.0.1 • Connectors 12.0.2 我在控制台 => 设置中添加了 graphdb.w
我正在尝试通过 jQuery 调用 Sonar 网络服务之一。由于调用是跨域进行的,因此调用在 Chrome 上失败并出现以下错误: 请求的资源上不存在“Access-Control-Allow-Or
我想使用 NestJs api,但我在每个 fetch 中都收到相同的错误消息: Access to fetch at 'http://localhost:3000/articles' from or
我不确定这是否属于这里,但我在开发我的 svelte 应用程序时遇到了问题。 在开发过程中,它目前在独立服务器上运行(遵循使用 rollup 和 sirv 的指南)并针对不同端口上的后端 API。 稍
如果在服务器上正确设置 CORS 以仅允许某些来源访问服务器,这是否足以防止 XSRF 攻击? 最佳答案 更具体地说,很容易错误地认为如果 evil.com 由于 CORS 无法向 good.com
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我是一名优秀的程序员,十分优秀!