跨域bug修复1
This commit is contained in:
@@ -202,3 +202,23 @@
|
||||
- 在 `clearAuth()` 调用后、路径检查之前,添加 `fetch('/api/clear_session.php', { method: 'POST', headers: { 'Content-Type': 'application/json' } })` 调用
|
||||
- 使用 try-catch 包裹,失败时仅输出 console.warn 警告,不阻塞后续重定向逻辑
|
||||
- `/api/clear_session.php` 是同源路径(由 Nginx 直接处理,不经过后端 FastAPI),不需要 Authorization header,也不会触发跨域问题
|
||||
|
||||
### 阶段 9:修复 500 响应 CORS 头丢失 + admin.js 变量重复声明
|
||||
|
||||
- [x] 9.1 修复 500 响应 CORS 头丢失
|
||||
【目标对象】`backend/middleware/auth_middleware.py`
|
||||
【修改目的】当路由层抛出异常导致 500 时,BaseHTTPMiddleware 的 call_next 返回的 500 响应不会经过 CORSMiddleware,导致 CORS 头缺失。浏览器报告"CORS Missing Allow Origin",前端无法读取错误信息。
|
||||
【修改方式】在 dispatch 方法中将 `return await call_next(request)` 改为 try-except 包裹,确保所有响应都有 CORS 头
|
||||
【修改内容】
|
||||
- 用 try-except 包裹 `call_next(request)` 调用
|
||||
- 在 try 中获取 response 后检查是否已有 CORS 头,若无则补充
|
||||
- 在 except 中捕获路由层异常,返回带 CORS 头的 500 响应
|
||||
|
||||
- [x] 9.2 修复 admin.js selectedStudentIds 重复声明
|
||||
【目标对象】`frontend/assets/js/admin.js`、`frontend/admin/conduct.php`、`frontend/admin/homework.php`
|
||||
【修改目的】admin.js 通过 footer.php 在所有 admin 页面加载,其 `let selectedStudentIds = []` 与 conduct.php 和 homework.php 页面级 `<script>` 中的同名声明冲突,导致 SyntaxError
|
||||
【修改方式】将 `let` 改为 `var`,`var` 允许重复声明
|
||||
【修改内容】
|
||||
- admin.js 第12-16行:`let` → `var`(4个变量声明)
|
||||
- conduct.php 第59行:`let selectedStudentIds = []` → `var selectedStudentIds = []`
|
||||
- homework.php 第94行:`let selectedStudentIds = []` → `var selectedStudentIds = []`
|
||||
|
||||
Reference in New Issue
Block a user