1
0
Files
frost-zx.github.io/docs/content/description-cors-preflight-request.md
2025-10-13 10:20:34 +08:00

46 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "CORS 预检请求preflight request"
date: 2025-03-15T23:08:14Z
lastmod: 2025-03-15T23:08:41Z
tags: [Web 前端,JavaScript,HTTP]
---
# CORS 预检请求preflight request
## 内容来源
- [Preflight request - 术语表 | MDN](https://developer.mozilla.org/zh-CN/docs/Glossary/Preflight_request)
- [跨源资源共享CORS - HTTP | MDN](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS)
## 简介
CORS 预检请求主要用于检测服务器是否支持 CORS跨域资源共享
当有必要的时候,浏览器会自动发出一个预检请求;因此在正常情况下,前端开发者不需要自己去发这样的请求。
## 触发条件
某些请求(以下称其为 “简单请求”)不会触发 CORS 预检请求。
若请求满足所有下述条件,则该请求可视为 “简单请求”:
- 使用下列请求方式之一
- GET
- HEAD
- POST
- 除了被用户代理自动设置的 Header例如 Connection、User-Agent和其他不在 Fetch 规范中定义的 [禁用的 Header 名称](https://fetch.spec.whatwg.org/#forbidden-header-name) ,允许人为设置下列 [对 CORS 安全的 Header 字段](https://fetch.spec.whatwg.org/#cors-safelisted-request-header)
- Accept
- Accept-Language
- Content-Language
- Content-Type需要注意额外的限制
- 在 Header 中,`Content-Type` 的值为下列之一
- text/plain
- multipart/form-data
- application/x-www-form-urlencoded
- 请求中的任意 `XMLHttpRequest` 对象均没有注册任何事件监听器;可以通过 `XMLHttpRequest.upload` 属性访问 `XMLHttpRequest` 对象。
- 请求中没有使用 `ReadableStream` 对象。