# 01 · CORS 与同源 **演示**:同源 `fetch`、跨源简单请求、跨源 PUT/自定义头触发预检、带凭证时 `allow-origin` 必须回显具体源。 **怎么看**:`node scripts/serve.mjs` 后打开 ,开 Console。 **预期输出**: ```text 同源 GET 状态:200,type=basic 同源请求不需要预检:{"preflight":false,"allowOrigin":"same-origin","readable":true} 跨源 PUT + JSON:{"preflight":true,"allowOrigin":"*","readable":true} 跨源带凭证:allow-origin 必须回显具体源,不能是 *。 ``` **注意**:示例的跨源部分只是判定模型,没有真的跨源;要观察 CORS 报错,开两个端口: ```sh node scripts/serve.mjs 8080 node scripts/serve.mjs 8081 ``` 从 `http://localhost:8080/...` 的页面控制台执行 `fetch('http://localhost:8081/examples/.../index.html')`,会看到 `TypeError: Failed to fetch`,Network 面板里是 CORS 错误而 JS 拿不到原因。需 `http://` 协议(`file://` 的源是 `null`,CORS 行为更严格)。