Fw: [問題] 明明有進入程式裡的方法,頁面卻跳出404

看板Web_Design作者 (= =)時間3年前 (2020/07/27 22:25), 3年前編輯推噓1(104)
留言5則, 1人參與, 3年前最新討論串2/2 (看更多)
※狀況概述: 就是我透過spring和JQuery ajax,寫了一個簡單的動態下拉式選單的程式, 我在瀏覽器上輸入url,在debugger模式下可以看到執行緒停留在中斷點的位置, 但是瀏覽器卻顯示404,而我在程式裡撈的資料也沒有回到前端,也就是 前端的回呼函數沒有接到撈出來的資料。 ※程式碼: 前端: $(document).ready(function(){ $("#dropdown1").change(function() { //alert("ajax"); $.ajax({ type :"GET", url : "/TestSpringJQueryAjax1/goToTestController.do?method=queryCountry", data : { continentId : $("#dropdown1").val(), }, dataType: "json", mmsuccess : function(data) { //alert(data.list[0].countryId); //var i = 0; $("#dropdown2 option").remove(); data.list.forEach(function(value,index){ $("#dropdown2").append(new Option(data.list[index].countryName, data.list[index].countryId)); //i++; }); }, error:function(data){ alert(data);//進入這裡,並顯示404 } }) }) }); 後端: @ResponseBody public String queryCountry(HttpServletRequest request, HttpServletResponse response) { String continentId = request.getParameter("continentId"); List<Country> list = testDao.queryCountry(continentId); Map<String, Object> resultMap = new HashMap<String, Object>(); resultMap.put("list", list); Gson gson = new Gson(); String result = gson.toJson(resultMap); return result; } ※錯誤訊息: 後端沒跳任何錯誤訊息,只有前端顯示: http://localhost:8080/TestSpringJQueryAjax1/goToTestController.do ?method=queryCountry&continentId=2 404 (Not Found) ------------------------------------- 所以現在卡在為何明明有進入到程式裏面,卻仍然跳404錯誤, 而且既然有加上@ResponseBody這個註釋,但後端撈出來的資料 卻沒有寫進HttpResponse裡面,所以回到前端,回呼函數沒接收到撈出來的資料。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.208.115 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/java/M.1595859040.A.78A.html ※ 編輯: lueichun (1.162.208.115 臺灣), 07/27/2020 22:38:08

07/28 13:24, 3年前 , 1F
看起來是你request的body並沒有附上去
07/28 13:24, 1F

07/28 13:24, 3年前 , 2F
後端接不到body 就不認為你的request是有效url
07/28 13:24, 2F

07/28 13:24, 3年前 , 3F
你可以用fiddler看你的body到底附到哪裡去了
07/28 13:24, 3F

07/28 13:26, 3年前 , 4F
而且你是用get 卻用body傳參數 接不到是正常的
07/28 13:26, 4F

07/28 13:26, 3年前 , 5F
建議你把get跟post搞懂 還有api的route機制也看一下
07/28 13:26, 5F
文章代碼(AID): #1V7kFYxm (Web_Design)
文章代碼(AID): #1V7kFYxm (Web_Design)