post方法提交报400错误 xhr.send( ( options.hasContent && options.data ) || null );解决办法

本文详细解析了POST请求返回400错误的原因及三种常见解决方案:检查参数拼写错误,确认后端方法和参数注解正确配置,以及调试JQuery版本是否合适。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:post报400错误

三种解决方法

一.参数有没有拼写错误

如:questDescribe写成questDscribe

 $.post("saveQuestion",{topicId:type,quesName:title,questDscribe:content},function (data) {
                    if(data=1){
                        alert("提交成功");
                    }
                });

错误会显示为:
在这里插入图片描述

这个种错误,很简单

二.后台没有确定Post方法、@RequestParam

参考代码

@RequestMapping(value = "saveQuestion",method = RequestMethod.POST)
    @ResponseBody
    public boolean SaveQuestion(@RequestParam String topicId,@RequestParam String quesName,@RequestParam String quesDescribe){
       

三.Jquery版本太高或者太低

一般情况下,不会下很多版本JQuery
可以使用BootCDN在线JQuery调试版本,选择合适的版本
https://www.bootcdn.cn/jquery/

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>login</title> </head> <body> <form> 用户:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" name="submit" onclick="validateForm()"><br> </form> <script> function validateForm() { const username = document.getElementById("username").value.trim(); const password = document.getElementById("password").value; const usernameError = document.getElementById("usernameError"); const passwordError = document.getElementById("passwordError"); //alert(username+password); // 发送登录请求到服务器 loginRequest(username, password); } function loginRequest(username, password) { const xhr = new XMLHttpRequest(); xhr.open("POST", "test.php", true); // 使用 POST 方法发送登录请求 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // 定义请求完成后的回调函数 xhr.onreadystatechange = function () { if (xhr.readyState === 4) { // 请求完成 if (xhr.status === 200) { // 请求成功 const response = xhr.responseText; // 获取服务器返回的字符串 handleLoginResponse(response); // 处理登录结果 } else { alert("请求失败,请检查网络或服务器状态"); } } }; // 发送数据(用户名和密码) const data = `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`; xhr.send(data); } function handleLoginResponse(response) { // 根据服务器返回的字符串判断登录结果 if (response === "success") { alert("登录成功"); // 登录成功后的操作,例如跳转到首页 window.location.href = "home.html"; } else if (response === "fail") { alert("登录失败,请检查用户名或密码"); } else { alert("服务器返回未知结果"); } } </script> </body> </html>
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值