用户提交数据混乱如何定位?是否需检查前后端校验逻辑与API入参处理?
问题定位步骤
排查方向 | 检查方法 | 工具/代码 |
---|---|---|
前端校验 | 浏览器开发者工具→Network | console.log(formData) |
后端接收 | 日志原始数据 | file_put_contents('log.txt', print_r($_REQUEST, true)); |
API规范 | Swagger文档比对 | @OA\Parameter 注解校验 |
校验强化方案
javascript
// 前端示例(Vue) submitForm() { if (!this.$refs.form.validate()) return; axios.post('/api', this.formData, { params: { token: this.$store.token } }) }
php
// 后端示例(Laravel) $request->validate([ 'email' => 'required|email', 'phone' => 'regex:/^1[3-9]\d{9}$/' ]);
自动化测试脚本
python
# pytest模拟异常数据 def test_invalid_input(): response = requests.post(url, json={"email": "wrong"}) assert response.status_code == 400
更新时间:2025-06-02 14:36:51