POST 後臺404錯誤

今天寫論壇的用戶回覆出錯了,我在提交表單的時候希望後臺返回一個數據
向後臺POST表單信息的時候,後臺顯示POST url 404的錯誤

1.前端代碼如下

<form id="form1">
        <p> <strong>來說點兒什麼吧...</strong></p>
        <p><span> 您的姓名:</span>
          <input name="toName" type="text" id="toName" />`在這裏插入代碼片`
          *</p>
        <p><span>選擇頭像:</span> *</p>
        <p> <i>
      <input type="radio" value= "<%=basePath%>images/tou1.jpg" id= "1" name="toImage"  style="display:none">
      <label for="1"><img class="img-circle"  id="a" src="<%=basePath%>images/tou1.jpg"  style="width: 60px;height: 60px" "changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou2.jpg" id= "2" name="toImage"  style="display:none">
      <label for="2"><img class="img-circle an" id="b" src="<%=basePath%>images/tou2.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou3.jpg" id= "3" name="toImage"  style="display:none">
      <label for="3"><img class="img-circle" id="c" src="<%=basePath%>images/tou3.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou4.jpg" id= "4" name="toImage" style="display:none">
      <label for="4"><img class="img-circle" id="d" src="<%=basePath%>images/tou4.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou5.jpg" id= "5" name="toImage" style="display:none">
      <label for="5"><img class="img-circle" id="e" src="<%=basePath%>images/tou5.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou6.jpg" id= "6" name="toImage" style="display:none">
      <label for="6"><img class="img-circle" id="f" src="<%=basePath%>images/tou6.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i>
      <input type="radio" value= "<%=basePath%>images/tou7.jpg" id= "7" name="toImage" style="display:none">
      <label for="7"><img class="img-circle" id="g" src="<%=basePath%>images/tou7.jpg"  style="width: 60px;height: 60px""changimge(this.id)"></label></i> <i></i>
    <p><span class="tnr">留言內容:</span></p>
      <textarea name="content" cols="60" rows="12" id="content"></textarea>
    </p>
    <input name="blogId" type="hidden" id="blogId" value="${blog.id }" />
    </form>
    <p>
      <input type="button"  onclick ="createtourist()" value="提交" />
    </p>

2.js代碼

//新建留言
	function createtourist() {
	var toName=$("#toName");
	var content = $("#content");
		 if (toName == "") {
		      alert("請輸入暱稱");
		      $("#toName").focus();
		      return false;
		  }
		  if (content.val().length < 5) {
		      alert("字數太短了~");
		      $("#content").focus();
		      return false;
		  }
	$.post("<%=basePath%>reply/toreply.action",
	$("#form1").serialize(),function(data){
	        if(data =="OK"){
	            alert("留言成功!");
	            window.location.reload();
	        }else{
	            alert("留言失敗!");
	            window.location.reload();
	        }
	    });
	}

3.後端代碼

@Controller
public class TouristController {
	//依賴注入
		@Autowired
		private TouristService touristService;
		@RequestMapping(value="/reply/toreply.action")
		public String CreateTourist(Tourist tourist,Model model){
			Date date = new Date();
			// 得到一個timestamp格式時間,存入mysql中的時間格式"yyyy/MM/dd HH:mm:ss"
			Timestamp timestamp = new Timestamp(date.getTime());
			//給主鍵賦值
			String id = UUID.randomUUID().toString();
			tourist.setRetime(timestamp);
			tourist.setId(id);
			int row = touristService.createTourist(tourist);
			if(row>0)
			{
			return "OK";
			}
			else{
			return "FAIL";
			}
		}
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章