这是我的javascript代码
<script type="text/javascript">
function callMe() {
var districtId = $("#district").val();
alert(districtId);
$.ajax({
type: "POST",
url: "addBranch",
data: "districtId=" + districtId,
success: function(response) {
}
});
}
</script>
这是我的控制器代码
@RequestMapping(value = "/addBranch", method = RequestMethod.POST)
public @ResponseBody
List getForm1(@ModelAttribute Branch branch, Model model,@RequestParam("districtId")
int districtId) {
try {
districtVillageList = villageService.getDistrictVillageList(districtId);
} catch (Exception er) {
log.error("error in addLoanType=" + er);
}
return districtVillageList;
}
我在控制器中获取列表,但由于我是ajax的新手,我不知道如何检索ajax中的列表并在jsp中使用检索到的值...请问任何人可能吗?< / p>
答案 0 :(得分:1)
当您返回控制器中的列表时,只需获取响应并使用for-in
循环进行迭代
如下所示,
<script type="text/javascript">
function callMe() {
var districtId = $("#district").val();
alert(districtId);
$.ajax({
type: "POST",
url: "addBranch",
data: "districtId=" + districtId,
success: function(response) {
for (var i in response){
// response[i].getterMethodHere
}
}
});
}
</script>
尝试使用json对象发送响应而不是列表。从jquery loop on Json data using $.each和jQuery loop over JSON result from AJAX Success?
了解详情