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

作者: lueichun (no anonymous)   2020-07-27 22:10:38
※狀況概述:
就是我透過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)
作者: pttworld (批踢踢世界)   2020-07-28 06:38:00
有@GetMapping嗎
作者: bitlife (BIT一生)   2020-07-28 11:10:00
測get method時先不透過ajax,直接用瀏覽器網址列輸入完整request url,然後看輸出.後端那裏(用斷點後印到console檢視result的內容,這算基本unit test或
作者: swallowcc (guest)   2020-07-28 16:32:00
砍掉,然後寫相同的東西就能跑了?
作者: l5125 (喲呼)   2020-07-28 18:24:00
漏了@Controller跟@GetMapping嗎?直接在網頁輸入訪問的地址應該會直接在網頁上輸出JSON的字串
作者: tw11509 (John-117)   2020-07-28 22:04:00
如果是用xml設定,請一起提供給大家看,要不然我們很難定位問題如果有用Java Configuration,也提供相關source code

Links booklink

Contact Us: admin [ a t ] ucptt.com