1. 程式人生 > >(7)ajax傳送put請求問題

(7)ajax傳送put請求問題

ajax傳送post請求,伺服器警告: Request method ‘POST’ not supported。
這裡寫圖片描述
為什麼會就不能像傳送get請求那樣,舒舒服服的得到預期的效果呢?

(1)下面往下看

1 給出jsp頁面傳送ajax請求的程式碼

$(document).on('click',"#user_update_btn",function(){
        //1 驗證輸入的資訊是否合法
        alert('修改中...');
        //2 使用ajax傳送put請求
        $.ajax({
            url:"${pageContext.request.contextPath}/user/"
+$(this).attr("edit_id")+".do", type:"POST", data:$("#update_user_model from").serialize(),//輸出修改模態框中表單的序列化結果: success:function(result){ alert(result.msg); //關閉對話方塊 //回到本頁面 } }) })

2 controller中處理的請求程式碼

@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
    @ResponseBody
    public JasonMsg updateUser(User user,HttpServletRequest request){
        System.out.println("請求體中的password值:"+request.getParameter("password"));
         System.out.println(user);//使用者名稱跟密碼都是null
         userService.updateUser(user);
         return
JasonMsg.success(); }

3 web.xml中的過濾器配置

<!-- 使用restful風格的url,這裡將普通頁面的post請求轉為put或delete請求 -->
    <filter> 
        <filter-name>HiddenHttpMethodFilter </filter-name> 
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter </filter-name> 
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

4 當我在加上_method=put,傳送的post請求就可以被成功響應了

$(document).on('click',"#user_update_btn",function(){
        //1 驗證輸入的資訊是否合法
        alert('修改中...');
        //2 使用ajax傳送put請求
        $.ajax({
            url:"${pageContext.request.contextPath}/user/"+$(this).attr("edit_id")+".do",
            type:"POST",
            data:$("#update_user_model from").serialize()+"&_method=PUT",//輸出修改模態框中表單的序列化結果:
            success:function(result){
                alert(result.msg);
                //關閉對話方塊

                //回到本頁面
            }
        })
    })

contoller處理請求程式碼與上面給出的一樣,但是輸出卻讓我大失所望呀

這裡寫圖片描述
請求體中的password值:null
User [id=5, username=null, password=null],

另外,還贈送給我一個Exception:
這裡寫圖片描述
明明我的mybatis對映檔案的sql寫的好好的

<update id="updateUserByPrimaryKeySelective" parameterType="cn.hdu.entity.User">
        update user into password=#{password} where id=#{id}
    </update>

於是乎,就有疑問了:
傳送ajax請求的時候,data裡面的資料是正常,而controller卻拿不到這個資料(除了路徑中的資料id),這是誰在搞鬼呢?
(2)當直接傳送ajax的put請求時
jsp頁面

$.ajax({
            url:"${pageContext.request.contextPath}/user/"+$(this).attr("edit_id")+".do",
            type:"PUT",
            data:$("#update_user_model from").serialize(),//輸出修改模態框中表單的序列化結果:
            success:function(result){
                alert(result);
            }
        })

執行之後,伺服器報錯

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'into password=null where id=5' at line 1

除了id之外,伺服器接收的其他資料權威null,所以sql語句會報錯。
總之,問題是:
直接傳送ajax=put請求,請求體中有資料,
但是User物件封裝不上資料,導致controller得到除了id不為null,其他欄位全是null的資料,
然後根據id給非空的欄位password賦值為null,資料庫就當然不願意了
update user into password=null where id=#{id}
(3)接著就來分析原因
罪魁禍首就是tomcat了。
tomcat首先將請求體中的資料存進map裡,然後伺服器可以利用request.getParameter(“”)拿到map中的資料,但是這隻對get或post請求有效
而springMVC封裝POJO的時候,只是呼叫tomcat的request物件,使用request.getParameter(“”)拿到資料,而經過我測試,還是得不到我從jsp頁面傳來的引數,所以得出結論:tomcat識別不了put請求,就不存在存資料到map中的過程。
(4)那怎麼辦呢?

配置HttpPutFormContentFilter過濾器

<!-- 處理put等請求 -->
    <filter> 
        <filter-name>HttpPutFormContentFilter </filter-name> 
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> 
    </filter> 
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter </filter-name> 
        <url-pattern>*.do</url-pattern>
    </filter-mapping>