上QQ阅读APP看书,第一时间看更新
Cross-Origin Resource Sharing (CORS)
The most important application of this OPTIONS method is Cross-Origin Resource Sharing (CORS). Initially, browser security prevented the client from making cross-origin requests. It means a site loaded with the URL www.foo.com can only make API calls to that host. If the client code needs to request files or data from www.bar.com, then the second server, bar.com, should have a mechanism to recognize foo.com to get its resources.
This process explains the CORS:
- foo.com requests the OPTIONS method on bar.com.
- bar.com sends a header like Access-Control-Allow-Origin: http://foo.com in response to the client.
- Next, foo.com can access the resources on bar.com without any restrictions that call any REST method.
If bar.com feels like supplying resources to any host after one initial request, it can set Access control to * (that is, any).
The following is the diagram depicting the process happening one after the other: