0

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.

5
  • 3
    You need to return a response to the AJAX request. Commented May 29, 2019 at 15:12
  • ^^ That, and bear in mind that AJAX requests are asynchronous, which means that it will send the request and then immediately continue to execute the rest of your Javascript, before the response is received. Commented May 29, 2019 at 15:13
  • @SLaks and how can I do that? Commented May 29, 2019 at 15:14
  • Using ajax and an API sort of makes JSP redundant too (which IMO, a good thing), since you can just adapt the DOM to it Commented May 29, 2019 at 15:14
  • The jQuery AJAX method has .done() and .fail() methods that take anonymous functions. For example... $.ajax().done(function(response) { console.log(response); })); Read more about jQuery AJAX calls here Commented May 29, 2019 at 15:17

1 Answer 1

3

You need to use @ResponseBody and inside the ajax use the success callback to get the value of success of ajax.

DataTypeOfReturn is the type of the data which you want to return & it can be int/String

function edit(id) {
  jQuery.ajax({
    type: "GET",
    url: "getId",
    data: "id= " + id,
    datatype: "text",
    success: function(data) {
      console.log(data)
    }
  });
  var type = <%= ((String)request.getAttribute("myType"))%>;
  console.log("type is " + type);
}



@RequestMapping("/getId")
public @ResponseBody DataTypeOfReturn getId(
  @RequestParam int id, HttpServletRequest request) {
  int idBook = id; // add data type here to avoid java error
  System.out.println("get id book " + id);
  String type = BookDao.getTypeCategory(id);
  request.setAttribute("myType", type);
  System.out.println("request attribute" + request.getAttribute("myType"));
  return theValue; // value which you want to return
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.