React.js 中的 CORS
CORS in React.js
我是 CORS 概念的新手,我正在使用 React-Redux 开发一个项目。我需要与第三方 API 沟通,这无法通过 AJAX 完成。谁能解释一下我可以在支持 CORS 的 React 中使用哪个库?为什么它比其他的好?
如上所写here:
The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on user data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.
Subsequent sections discuss scenarios, as well as a breakdown of the HTTP headers used.
您应该在服务器端设置 CORS headers。如果您无权访问 API 代码,您可以为此创建一个代理服务器,它将代理请求到 API 并使用 CORS headers 发回响应。或者您可以在同一个域上设置代理服务器,并且不需要 CORS。
我是 CORS 概念的新手,我正在使用 React-Redux 开发一个项目。我需要与第三方 API 沟通,这无法通过 AJAX 完成。谁能解释一下我可以在支持 CORS 的 React 中使用哪个库?为什么它比其他的好?
如上所写here:
The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on user data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.
Subsequent sections discuss scenarios, as well as a breakdown of the HTTP headers used.
您应该在服务器端设置 CORS headers。如果您无权访问 API 代码,您可以为此创建一个代理服务器,它将代理请求到 API 并使用 CORS headers 发回响应。或者您可以在同一个域上设置代理服务器,并且不需要 CORS。