I have a edit funtion in javascript jsp in which i need to pass the id of a book to the java method. In the java method, i use that id to search through a table from database and to find the type of book. (category) Then, I need to go back to the jsp (javascript function) and to load that type of book into a field.
JAVASCRIPT inside JSP:
<script>
function edit(id) {
jQuery.ajax({
type: "GET",
url: "getId",
data: "id= " + id,
datatype: "text"
});
var type =<%= ((String)request.getAttribute("myType"))%> ;
console.log("type is " + type);
}
</script>
JAVA:
@RequestMapping("/getId")
public void getId(
@RequestParam int id,HttpServletRequest request) {
idBook = id;
System.out.println("get id book "+id);
String type= BookDao.getTypeCategory(id);
request.setAttribute("myType",type);
System.out.println("request attribute"+request.getAttribute("myType"));
}
By doing so, the type from javascript is null... How to change that? (the type from java is holding the wanted value). BookDao.getTypeCategory is using the id to search through the database table and to retrieve the needed type.
.done()and.fail()methods that take anonymous functions. For example...$.ajax().done(function(response) { console.log(response); }));Read more about jQuery AJAX calls here