POST 後臺404錯誤

今天在開發一個用戶信息更新模塊的時候遇到了兩個問題:
一、在我提交form表單的時候我但願在提交表單以後頁面不刷新,同時返回更新數據
二、向後臺POST表單信息的時候,後臺顯示POST url 404的錯誤javascript

解決第一個問題的使用使用jquery.form的插件進行異步提交java

<script type="text/javascript" src="jquery/jquery.js"></script></head>  
<script type="text/javascript" src="jquery/jquery.form.js"></script>  
$(function() {
            
            $("#user-update").submit(function(){
                $(this).ajaxSubmit({
                    type:"post",  //提交方式
                    dataType:"json", //數據類型
                    url:"${pageScope.basePath}user/update", //請求url
                    success:function(data){ //提交成功的回調函數
                        layer.alert("保存成功");
                    }
                });
                return false; //不刷新頁面
            });
        });

個人form表單是這個樣子的jquery

<form  class="am-form"  id="user-update" method="POST" >
          <fieldset>
            <h4>用戶信息</h4>
            <input value name="id" style="display:none"/>
            <div class="am-form-group">
              <label for="doc-vld-name-2">用戶名:</label>
              <input type="text" name="username" autocomplete="off" valuerequired/>
            </div>
            <div class="am-form-group">
              <label for="doc-vld-name-2">用戶中文名:</label>
              <input type="text" name="chineseName" autocomplete="off" value required/>
            </div>

            <div class="am-form-group">
              <label for="doc-vld-desc-2">用戶郵箱: </label>
              <input type="text"  name="email"  autocomplete="off" value  required/>
            </div>
            <div class="am-form-group">
              <label for="doc-vld-desc-2">用戶最後登陸時間:</label>
              <input type="text"   disabled='true' name="loginTime"   value required/>
            </div>
           <input class="am-btn am-btn-primary" type="submit" name="getresult"  value="更新" />
          </fieldset>
        </form>

使用這種方式便可對form進行異步提交,提交以後我發現後臺反回了以下錯誤
圖片描述ajax

個人後臺的java代碼以下,採用了spring mvc 的restful風格進行編寫的spring

@RequestMapping(value = "/update",method = RequestMethod.POST)
    public String updateUser(HttpServletRequest request,HttpServletResponse response) {
        
        response.addHeader("Access-Control-Allow-Origin", "*");
        /* 獲取前臺傳送的參數 */
        String id = request.getParameter("id");
        ......
        
        /* 組裝user */
        User user = new User();
        user.setId(Integer.parseInt(id));
        .......
        boolean updateResult = service.update(user);
        logger.debug("UserController+updateUser()-------->" + updateResult);
        return String.valueOf(updateResult);
    }

我發現不能返回我想要的字符串,因而使用@ResponseBody來返回數據(@responsebody表示該方法的返回結果直接寫入HTTP response body中通常在異步獲取數據時使用,在使用@RequestMapping後,返回值一般解析爲跳轉路徑,加上@responsebody後返回結果不會被解析爲跳轉路徑,而是直接寫入HTTP response body中。好比異步獲取json數據,加上@responsebody後,會直接返回json數據)因而返回結果成功。
@Responsebody原理
該註解用於將Controller的方法返回的對象,經過適當的HttpMessageConverter轉換爲指定格式後,寫入到Response對象的body數據區。注意到使用@ResponseBody將會跳過視圖處理部分,調用合適的HttpMessageConverter,將返回值寫入輸出流。json

相關文章
相關標籤/搜索