网站首页 > 教程文章 正文
一、问题描述:后端的接口用的 PUT 方式, 前端的提交数据需要用PUT方式
@RequestMapping(value = "/updateOnePersonById",method = RequestMethod.PUT) public Boolean updateOnePersonById(@RequestParam Map map){
...
}
如 果 前 端 提 交 的 方 式 和 后 端 指 定 的 方 式 不 一 致 , 经 常 会 出 现 Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported]
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
诸如以上的错误,都是提交方式不匹配引起的
二、解决方法:对于 post get 使用对应的提交方式即可;对应 PUT方式,提交的 type 写成 post,再添加一个 _method=PUT 即可
*需要对这个data做处理,在json 串中添加一个属性 * Vue 的写法:在 data 中个添加了一个 对象,存放表单数据,提交这个数据的时候,需要添加一个 _method : "PUT"
data: { updateForm: {},
}
...
**//需要对这个data做处理,在json 串中添加一个属性** this.updateForm['_method'] = 'PUT';
$.ajax({
url: "http://localhost:8080/updateOnePersonById", type: "post",
data: vm.updateForm, success: function(result) {
console.log("updateOnePersonByIdMsg", result); if (result) {
alert("修改成功");
//刷新数据,并停留在当前页vm.toPage(vm.pageInfo.pageNum);
} else {
alert("修改失败");
}
}
});
三、以上的方法,不用原生的ajax 提交的时候,用下面的方式提交的时候,又不行了
Vue.prototype.$http = axios
const result= await this.$http.put("http://localhost:8080/updateOnePersonById",vm.updateForm);
使用axios 方式提交的时候,有不行了
四、解决方案:
todo... 欢迎大佬提出解决方法笔者自己的解决方案
1)type 直接写成 put 2)同时设置 contentType:"application/json", 3)提交的数据转成 json data:JSON.stringify(vm.updateRoleAndPermission),
4)后端的controller requestMapping 加上 method = RequestMethod.PUT,produces =
{"application/json;charset=UTF-8"} 5)参数接收使用 @RequestBody 注解
亲测有效
猜你喜欢
- 2024-12-03 Yii::$app->request常用属性和方法
- 2024-12-03 查缺补漏:一文看懂HTTP请求流程,不信你还不会
- 2024-12-03 Mysql写入频繁,怎么破?这是我见过的最清晰的“神操作”
- 2024-12-03 “我将 AWS 的数据传输成本,降低了 99%!”
- 2024-12-03 必备 Python 库:Requests - 轻松完成 HTTP 请求
- 2024-12-03 免费获取韵达快递查询API的使用指南
- 2024-12-03 手撸了一个网络请求工具类,开发速度迅速提升了300%
- 2024-12-03 应用层协议HTTP和HTTPS,一篇文章学会
- 2024-12-03 HTTP客户端连接,选择HttpClient还是OkHttp?
- 2024-12-03 深入理解 HTTP 请求参数和响应参数
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)