Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)
本文装载自:http://blog.csdn.net/u012737182/article/details/52831008 感谢原文作者分享
开发环境:Tomcat9.0
在使用Ajax实现Restful的时候,有时候会出现无法Put、Delete请求参数无法传递到程序中的尴尬情况,此时我们可以有两种解决方案:1、使用地址重写的方法传递参数。2、配置web.xml项目环境。
测试的程序为:
@RequestMapping(value = "/member", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
public @ResponseBody Object edit(Member vo1) {
log.info("【*** 修改用户信息 ***】" + vo1);
JSONObject obj = new JSONObject();
obj.put("flag", true);
return obj;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
一、使用地址重写的方法来实现put、delete请求的参数传递。
在js页面中(
$(editMember).on("click",function(){
$.ajax({
url : "member?empno=1009&ename=阿伦&sal=19777.77&hiredate=1969-10-10" , // 处理的请求路径
type : "put" , // 此处发送的是PUT请求(可变更为其他需要的请求)
dataType : "json" , // 返回的数据类型为json类型
success : function(data) {
$(showDiv).append("<p>修改处理结果:" + data.flag + "</p>") ;
} ,
error : function(data) {
$(showDiv).append("<p>对不起,出错啦!</p>") ;
}
}) ;
}) ;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
二、使用配置文件修改来实现Put和Delete请求的参数传递
1、解决Put请求的参数传递,但是 无法解决 Delete 请求的传递
①、在项目中的web.xml文件中配置:
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
②在js文件中:
$(editBut).on("click",function(){
$.ajax({
url: "member",
type : "put", // 此处发送的是PUT请求
data : {
empno : 1170,
ename : "SMITH",
sal : 11.1,
hiredate : "1991-11-11"
},
success : function(data){
$(showDiv).append("<p> 数据更新成功:"+data.flag+"</p>");
console.log(1);
},
dataType : "json",
error : function(data){
$(showDiv).append("<p>对不起,出错啦!</p>");
}
})
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2、解决 Put和Delete 请求的参数传递。
①、在项目中的web.xml文件中配置:
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<!-- 备注,这边的名称必须和配置'springmvc'的servlet名称一样 -->
<servlet-name>springmvc</servlet-name>
</filter-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
②在js文件中:
$(editBut).on("click",function(){
$.ajax({
url: "member",
type : "post", // 此处发送的是POST请求
data : {
_method : "put", // 将请求转变为PUT请求
empno : 1170,
ename : "SMITH",
sal : 11.1,
hiredate : "11111-11-11"
},
success : function(data){
$(showDiv).append("<p> 数据更新成功:"+data.flag+"</p>");
console.log(1);
},
dataType : "json",
error : function(data){
$(showDiv).append("<p>对不起,出错啦!</p>");
}
})
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)的更多相关文章
- 资料汇总--Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)【转】
开发环境:Tomcat9.0 在使用Ajax实现Restful的时候,有时候会出现无法Put.Delete请求参数无法传递到程序中的尴尬情况,此时我们可以有两种解决方案:1.使用地址重写的方法传递参数 ...
- Node.js中针对中文的查找和替换无效的解决方法
Node.js中针对中文的查找和替换无效的解决方法. //tags的值: tag,测试,帖子 var pos1 = tags.indexOf("测"); //这里返回-1 ta ...
- php form表单提交时,action url中参数无效的解决方法
表单提交时get方式的一个错误 <form class="form-inline pull-right" method="get" action=&quo ...
- jQuery中hover和blur使用代理delegate无效的解决方法
今天就遇到了这样的小问题: $(document).ready(function(){ $('.status_on').hover(function(){ $(this).html('点击禁用'); ...
- IPhone中H5页面用on绑定click无效的解决方法
首先声明本人资质尚浅,本文只用于个人总结.如有错误,欢迎指正.共同提高. --------------------------------------------------------------- ...
- spring boot:方法中使用try...catch导致@Transactional事务无效的解决(spring boot 2.3.4)
一,方法中使用try...catch导致@Transactional事务无效的解决方法 1,问题的描述: 如果一个方法添加了@Transactional注解声明事务, 而方法内又使用了try catc ...
- 【转】AJAX发送 PUT和DELETE请求注意事项
jax使用restful服务发送put 和 delete 请求时直接传参会出现问题 一,采用POST + _method:delete/put + filter 的方法ajax发送put 和 de ...
- AJAX发送 PUT和DELETE请求参数传递注意点,了解一下
ajax发送put 和 delete 请求时,需要传递参数,如果参数在url地址栏上,则可以正常使用, 如果在 data:中需要传递参数,(浏览器会使用表单提交的方式进行提交) 则需要注意此时应作如下 ...
- 原生ajax中get和post请求
后台代码: class AjaxHanlder(tornado.web.RequestHandler): def get(self): print(self.get_argument('type',N ...
随机推荐
- MST-prim ElogV
#include<bits/stdc++.h> #define ll long long using namespace std; ; ; struct node { int t;int ...
- 2017 CVTE Windows开发二面 3.8 (offer)
中午1点左右,广州的号码打过来了,是CVTE的hr,然后问我下午4点半有没有时间,帮我约视频的二面. 当然有时间了啦,然后hr给我邮箱发了个链接,让我4点半登陆进去. 因为1面没问任何网络和操作系统的 ...
- WDigest
WDigest 是windows在XP系统中应用的,其作用主要是与超文本传输协议(HTTP)和简单的身份验证安全层(SASL)一起交换使用.而Digest与NTLM协议类似,也是挑战认证协议. 简单说 ...
- react 中的路由 Link 和Route和NavLink
route是配置,link是使用 https://blog.csdn.net/chern1992/article/details/77186118(copy) 嵌套路由一般使用Route,类似于vue ...
- HNOI2010 平面图判定(planar)
题目链接:戳我 我怎么知道平面图有这个性质?? 对于一个平面图,它的边数不超过点数的\(3n-6\) 所以可以直接把边数多的特判掉,剩下的图中边数和点数就是一个数量级的了. 因为这个图存在欧拉回路,所 ...
- Springboot 默认静态路径
springboot 默认静态路径 代码如下所示 类ResourceProperties.class private static final String[] CLASSPATH_RESOURCE_ ...
- Codeforces 1213C Book Reading
cf题面 中文题意 多组数据,每组给一个n给一个m,要求正整数\(1\)~\(n\)中,所有能被m整除的数的个位之和. 解题思路 首先,能被m整除的数的数量是\(\lfloor\frac{n}{m}\ ...
- sh_10_字典基本使用
sh_10_字典基本使用 xiaoming_dict = {"name": "小明"} # 1. 取值 print(xiaoming_dict["na ...
- CodeForces 349B--Color the Fence(贪心)
B. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- G.subsequence 1(dp + 排列组合)
subsequence 1 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 You are ...