这是我在javascript中的变量:
var dataid = dataInfo[i];
我想通过href:
将变量传递给我的java控制器 row = row + "<tr><td>" + dataid + "</td><td>" +
schoolid + "</td><td>" +
"<td><a class='details' id='" + dataid + "' href='@{DataManagement.dataDetails(dataId)}'>Details</a></td>"+
"<td>"+
</tr>";
但控制器获取空值。
我正在尝试使用ajax:
$.ajax({
type: "GET",
url: "@{DataManagement.dataDetails}",
data: {
id: dataId
},
success: function(data) {
console.log(data);
}
});
这是我的控制者:
public static void dataDetails(Long id) throws SQLException {
Logger.info("id: "+ id);
//dataId=dataId.trim();
//Long iid = Long.parseLong(dataId);
Data data = Data.findById(id);
String totalStudent = Data.getTotalStudent(1L);
Logger.info("totalStudent: " + totalStudent);
renderArgs.put("totalStudent",totalStudent);
render(data,totalStudent);
}
但是在ajax调用之后它不会呈现新页面。
我该怎么做?
答案 0 :(得分:1)
它有效!!!
我把它改为:
<td><a class='details' id='" + dataid + "' href='/datamanagement/dataDetails/"+dataid+"'>Details</a></td>
答案 1 :(得分:0)
您可以使用ajax发送json请求并在Controller上接受它。
Ajax请求:
$.ajax({
type: "POST",
contentType: "application/json",
data: JSON.stringify(yourObject),
url: "/path",
success: function (msg) {
console.log(msg);
window.location = "/redirectTo";
},
error : function(e) {
console.log('Error: ' + e);
}
});
在控制器上:
@ResponseBody
@CrossOrigin @RequestMapping(value = "/path", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
private ResponseEntity<YourObject> someMethod(@RequestBody YourObject obj){
// do your coding here
return new ResponseEntity<YourObject>(modifiedObject, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<YourObject>(HttpStatus.BAD_REQUEST);
}
}