Reputation: 315
This is my variable in javascript:
var dataid = dataInfo[i];
I want to pass the variable to my java controller through href:
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>";
but controller gets null value.
I am trying this using ajax:
$.ajax({
type: "GET",
url: "@{DataManagement.dataDetails}",
data: {
id: dataId
},
success: function(data) {
console.log(data);
}
});
This is my controller:
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);
}
But after ajax call it is not render the new page.
How can I do this?
Upvotes: 0
Views: 1067
Reputation: 315
It works!!!
I changed it to :
<td><a class='details' id='" + dataid + "' href='/datamanagement/dataDetails/"+dataid+"'>Details</a></td>
Upvotes: 1
Reputation: 41
You can send a json request with ajax and accept it on Controller.
Ajax Request:
$.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);
}
});
On Controller:
@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);
}
}
Upvotes: 0