title: Is it possible for a Java controller to pass an attribute to a success ajax call?
body: <p>As the title says, I want to pass a variable from a controller to a success ajax call function but is it possible? For example:</p>
  
  <p>I have a Java class with a controller like this:</p>
  
  <pre><code>@RequestMapping(value = "checkFruit", method = RequestMethod.POST)
  @ResponseBody
  public ModelAndView checkFruit(HttpServletRequest request, String fruitInput) {
  
      ModelAndView modelAndView = new ModelAndView(); 
  
      if (fruitInput.equals("apple")) {
          modelAndView.addObject("fruitType", 1); //This is what I want to pass to the success ajax call.
      } else if (fruitInput.equals("orange") {
          modelAndView.addObject("fruitType", 2); //This is what I want to pass to the success ajax call.
      }
  
      modelAndView.setViewName("fruitGarden");
      return modelAndView;
  }
  </code></pre>
  
  <p>And a jsp view with an ajax call like this:</p>
  
  <pre><code>$("#checkFruitBtn").click(function () {
      var fruitInput = $("input[name=fruitInput]").val();
      $.ajax({
          type: 'POST',
          url: 'checkFruit',
          data: 'fruitInput=' + fruitInput,
          success: function () {
              var fruitType= ${fruitType}; //I want to get the attribule of that "fruitType" variable set in the controller. But this is where things went wrong.
              if (fruitType == 1) {
                  alert("This is an apple.");
              } else if (fruitType == 2) {
                  alert("This is an orange.");
              }
              window.location.href = "/someURL";
          }
      });
  });
  </code></pre>
  
  <p>In the above example. When I click the button "checkFruitBtn", it will send an ajax call to the controller "checkFruit". And in this controller, I'll set a variable "fruitType" to send it to the success ajax call function. In this success ajax call function, I'll get the value of the "fruitType" variable to check it and show alert message according to its value... But, things aren't going as planned.</p>
  
  <p>My question is, is there a possible way of getting the value of that "fruitType" variable? I've searched for a getting method but I still can't find one that fitted in my case. I'm terribly sorry if this question have been asked before.</p>
  
  <p>Thanks in advance!</p>