我在 Jsp One 中有两个下拉列表,一个用于州,另一个用于国家。一旦我选择了国家,statelist 就会自动填充相应的列表。但是我在 ajax 调用中得到整个 jsp 页面作为响应。 我的 ajax 程序:
$("select#country").change(function() {
var val=$("#country").val();
alert(val);
$.ajax({
url : 'getstates',
method : 'get',
contentType: 'application/json',
data :{
country : val
},
success: function (data) {
alert("Success Response"+ data);
},
error :function()
{
alert("error");
}
});
我的 Controller 程序
@RequestMapping(value = "/getstates", method = RequestMethod.GET)
public ModelAndView showstates(@RequestParam(required = false, value = "")
String country,@Valid @ModelAttribute("employee")Login employee,
BindingResult result, Model model) {
HashMap<String,String> stateMap = new HashMap<String,String>();
//put your logic to add state on basis of country
if (country.equals("UnitedStates")) {
stateMap.put("Al","Alaska");
stateMap.put("Tl","Texas");
} else if (country.equals("UnitedKingdom")) {
stateMap.put("Be","Bedfordshire");
stateMap.put("Ber","Berkshire");
} else if (country.equals("India")) {
stateMap.put("Mh","Maharashtra");
stateMap.put("WB","West Bengal");
stateMap.put("KR","Karnataka");
stateMap.put("AP","Andhra Pradesh");
stateMap.put("TN","Tamil Nadu");
}
return new ModelAndView("LoginForm","state" ,stateMap);
}
我正在使用 Spring 形式。我只需要获取 Staemap 作为响应,但我正在获取整个 jsp 页面作为响应。
最佳答案
I need to get only Staemap as respone but i am getting whole jsp page as response.
因为您要返回带有 View 的 ModelAndView
对象,
return new ModelAndView("LoginForm","state" ,stateMap);
如果您需要从 Controller 方法中单独返回 respone
。但是您不能在 ajax
响应中直接打印 HashMap
jsp。恕我直言,您可以将其转换为 JSONArray
JSONArray jarray = JSONArray.fromObject(statemap);
阅读,
关于java - 在 Spring Mvc 中将 Hashmap 从 Controller 返回到 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26796352/