Skip to content

Commit

Permalink
Updated README.md and README_ZH.md (#1768)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Alex Li <[email protected]>
  • Loading branch information
Shuo-Li and AlexV525 committed Mar 28, 2023
1 parent 302343f commit f11f5f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dio 是一个强大的 Dart HTTP 请求库,支持全局配置、Restful API、
* [HTTP/2 支持](#http2-支持)
* [请求取消](#请求取消)
* [继承 Dio class](#继承-dio-class)
* [Web 平台跨域资源共享 (CORS)](#web-平台跨域资源共享--cors-)
<!-- TOC -->
</details>

Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
<!-- TOC -->
</details>

Expand Down Expand Up @@ -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

0 comments on commit f11f5f0

Please sign in to comment.