From f11f5f00e82e31c12e0df40e128a2c83816b7fad Mon Sep 17 00:00:00 2001 From: Shuo Li <6397910+Shuo-Li@users.noreply.github.com> Date: Tue, 28 Mar 2023 02:30:50 -0500 Subject: [PATCH] Updated README.md and README_ZH.md (#1768) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated both README.md and README_ZH.md to add a small section: CORS on Flutter Web; Web平台CORS ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [ ] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [ ] I have run the tests without failures - [ ] I have updated the `CHANGELOG.md` in the corresponding package --------- Signed-off-by: Shuo Li <6397910+Shuo-Li@users.noreply.github.com> Co-authored-by: Alex Li --- dio/README-ZH.md | 12 ++++++++++++ dio/README.md | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/dio/README-ZH.md b/dio/README-ZH.md index 7d86c936e..5bdc0e3e2 100644 --- a/dio/README-ZH.md +++ b/dio/README-ZH.md @@ -46,6 +46,7 @@ dio 是一个强大的 Dart HTTP 请求库,支持全局配置、Restful API、 * [HTTP/2 支持](#http2-支持) * [请求取消](#请求取消) * [继承 Dio class](#继承-dio-class) + * [Web 平台跨域资源共享 (CORS)](#web-平台跨域资源共享--cors-) @@ -853,3 +854,14 @@ class MyDio with DioMixin implements Dio { // ... } ``` + +## Web 平台跨域资源共享 (CORS) + +在 Web 平台上发送网络请求时,如果请求不是一个 [简单请求][], +浏览器会自动向服务器发送 [CORS 预检][] (Pre-flight requests), +用于检查服务器是否支持跨域资源共享。 + +你可以参考简单请求的定义修改你的请求,或者为你的服务加上 CORS 中间件进行跨域处理。 + +[简单请求]: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS#%E7%AE%80%E5%8D%95%E8%AF%B7%E6%B1%82 +[CORS 预检]: https://developer.mozilla.org/zh-CN/docs/Glossary/Preflight_request diff --git a/dio/README.md b/dio/README.md index ad02e88ba..3e0d430c5 100644 --- a/dio/README.md +++ b/dio/README.md @@ -47,6 +47,7 @@ timeout, and custom adapters etc. * [HTTP/2 support](#http2-support) * [Cancellation](#cancellation) * [Extends Dio class](#extends-dio-class) + * [Cross-Origin Resource Sharing on Web (CORS)](#cross-origin-resource-sharing-on-web--cors-) @@ -902,3 +903,16 @@ class MyDio with DioMixin implements Dio { // ... } ``` + +## Cross-Origin Resource Sharing on Web (CORS) + +If a request is not a [simple request][], +the Web browser will send a [CORS preflight request][] +that checks to see if the CORS protocol is understood +and a server is aware using specific methods and headers. + +You can modify your requests to match the definition of simple request, +or add a CORS middleware for your service to handle CORS requests. + +[simple request]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests +[CORS preflight request]: https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request