我在Jsp One中有两个下拉列表用于州和其他国家/地区。只要我选择国家/地区,就应该使用相应的列表自动填充州名单。但我得到整个jsp页面作为ajax调用中的响应。 我的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");
}
});
我的控制器程序
@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);
}
我正在使用弹簧形式。我只需要将Staemap作为respone,但我将整个jsp页面作为响应。
答案 0 :(得分:1)
我只需要将Staemap作为respone但我正在获取整个jsp页面 作为回应。
因为您在此处返回ModelAndView
对象,
return new ModelAndView("LoginForm","state" ,stateMap);
如果您需要单独从控制器方法返回respone
。但是,您无法直接在jsp上的HashMap
响应中打印ajax
。恕我直言,你可以将其转换为JSONArray
JSONArray jarray = JSONArray.fromObject(statemap);
阅读,
答案 1 :(得分:0)
@RequestMapping(value="LoadBaselineVersions")
@ResponseBody
public Map<Integer,String> loadBaseNames(@RequestParam(value="projectname") String name,HttpSession session){
return basenameService.getBaselineversions(name);
}
$("#projectname").bind(
'blur',
function() {
$.ajax({
type : 'post',
url : 'LoadBaselineVersions?projectname='
+ $("#projectname").val(),
dataType : 'json',
success : function(data) {
$("#baseversion").empty();
$.each(data, function(val, text) {
$("#baseversion").append(
$('<option></option>').val(text).html(
text));
});
}
});
});