JavaScript Method to call Spring MVC controller method through ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satishch
    New Member
    • Apr 2013
    • 1

    JavaScript Method to call Spring MVC controller method through ajax

    I would like to call spring mvc method when ever onUnload.
    So i write the code

    in <body onUnload="bye() "></body>
    In javascript
    -------------
    Code:
    <script type="text/javascript">
            	function bye(){
            		alert("Before Ajax..!");
            		$.ajax({  
            	        type : 'GET',  
            	        url : "/patient/myMealPlan",  
            	        
            	    });
            		alert("onUnLoad Bye");
            	}
            </script>

    In spring MVC Controller class
    --------------------------------
    Code:
    @RequestMapping(value="myMealPlan", method=RequestMethod.GET)
    	public String init(ModelMap map, HttpServletRequest request){
    	System.out.println("Bye method called..!");
    In java-script alert()'s are calling properly but coming to spring MVC controller will not call.
    Why it is not called ?
    Is there any .js file we need to import in .jsp file ?
    if so which file .js file i would like to import?
    Last edited by Rabbit; Apr 20 '13, 12:16 AM. Reason: Please use code tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Is there any .js file we need to import in .jsp file ?
    if so which file .js file i would like to import?
    JavaScript runs on the client as Spring runs on the server … so you don’t need to load any JS into Spring (I even doubt Spring could do something useful with a JS file).

    hm, and since you call the JS function on unload, how should you see the server response?

    Comment

    Working...