我有 2 个下拉菜单的问题(第二个框选项取决于当前选择的第一个框选项)。我现在拥有的是: 控制器:
@Controller
@RequestMapping(ADMINISTRATION)
{...}
@RequestMapping(value = USERS + STUDENT + "/reloadGroups/{schoolId}", method = GET)
public @ResponseBody
List<StudyGroup> reloadGroups(@PathVariable(value = "schoolId") final int schoolId) {
return studyGroupService.getStudyGroupBySchool(schoolService.getSchool(schoolId));
}
html:
<script type="text/javascript">
$(document).ready(
function() {
var selectSchool = $("#school");
var selectGroup = $("#studyGroup");
selectSchool.change(function() {
var schoolId = $("#school option:selected").val();
$.get("/administration/users/student/reloadGroups/"
+ schoolId, function(data) {
selectGroup.empty();
console.log(data);
var group = JSON.parse(data);
for ( var option in data) {
selectGroup.append($("<option/>", {
text : group[option].name,
key : group[option].id
}));
}
})
})
});
</script>
//and of course 2 select boxes with ids 'school' and 'studyGroup'
我收到如下错误:
"NetworkError: 404 Not Found - http://localhost:8080/administration/users/student/reloadGroups/2"
但我不知道为什么,请求映射应该没问题。例如,当我在浏览器中输入http://localhost:8080/drdiet-web/administration/users/student/reloadGroups/1
错误更改为 500 时:
"NetworkError: 500 Internal Server Error - http://localhost:8080/drdiet-web/administration/users/student/reloadGroups/1"
有什么问题?我是阿贾克斯的新手。
编辑
这之前“有效”,但不完全是,因为我只能更改第一个下拉值,因此它影响了第二个可用选项:
$(document).ready(function() {
var selectSchool = $("#school");
var selectGroup = $("#studyGroup");
selectSchool.change(function(){
var schoolId = $("#school option:selected").val();
$.ajax({
url: config.baseUrl + '/administration/users/student/reloadGroups/' + schoolId,
type: 'GET',
data: schoolId,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: 'text',
error : function() {
alert("error");
console.log("error");
},
success: function(data){
selectGroup.empty();
var aaa = JSON.parse(data);
console.log(data);
alert("ok");
for(var opt in aaa){
selectGroup.append($("<option/>", {
text: aaa[opt].name,
key: aaa[opt].id
}));
console.log(aaa[opt]);
}
}
})
});
});