資料彙總--Ajax中Put和Delete請求傳遞參數無效的解決方法(Restful風格)【轉】

開發環境:Tomcat9.0 
在使用Ajax實現Restful的時候,有時候會出現沒法Put、Delete請求參數沒法傳遞到程序中的尷尬狀況,此時咱們能夠有兩種解決方案:一、使用地址重寫的方法傳遞參數。二、配置web.xml項目環境。javascript

 

測試的程序爲:java

@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、使用地址重寫的方法來實現put、delete請求的參數傳遞。 
在js頁面中(web

$(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>") ;
            } 
        }) ;
    }) ;

 

2、使用配置文件修改來實現Put和Delete請求的參數傳遞 
一、解決Put請求的參數傳遞,可是 沒法解決 Delete 請求的傳遞 
①、在項目中的web.xml文件中配置:ajax

<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>

②在js文件中:spring

$(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>");
            }
        })
    });

二、解決 Put和Delete 請求的參數傳遞。 
①、在項目中的web.xml文件中配置:json

<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>  

②在js文件中:mvc

$(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>");
            }
        })
    });
相關文章
相關標籤/搜索