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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM