Link to home
Create AccountLog in
Avatar of davidw88
davidw88

asked on

Ajax Parameter not passed

Hello experts,

I created a Spring MVC + Ajax prototype, however I found that the controller could not get the values passed in by the Ajax. Can anybody help to check?

The prototype works except the ajax part.

Thanks so much.
Ajax:

$(document).ready(
       function(){
		$("#expand_aid").click(	
						
			$.ajax({
				type: "POST",
				url: "http://localhost:8080/SpringMVC/welcome.htm",

				data: "customerId=22&ajax=true",
				dataType: 'json',
				async: true,
				cache: false,
				success: function(msg){
					alert ("Msg from ajax: -> " + msg);
				}
			});
			
		);

	}	
);
			

in controller:

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		String isAjax = arg0.getParameter("ajax");
		String customerId = arg0.getParameter("customerId");
		
		if (isAjax != null)
			return new ModelAndView("welcomePage", "welcomeMessage", customerId + "  " + ajaxMessage);
		
		return new ModelAndView("welcomePage", "welcomeMessage", message);
	}
}

Open in new window

Avatar of davidw88
davidw88

ASKER

Why

String isAjax = arg0.getParameter("ajax");
String customerId = arg0.getParameter("customerId");

do not work?

Avatar of Dave Baldwin
Are you posting to an 'htm' HTML page?  POST data does not appear in the URL so how would you know whether it was sent or not?  POST requires a server side language like PHP or ASP to read the POST data sent to the server.
Thanks for your reply.


At the server side it is a controller (java) since this is Spring MVC.

This is the post url:  "http://localhost:8080/SpringMVC/welcome.htm".


Following is my server side code.

      @Override
      protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
                  HttpServletResponse arg1) throws Exception {
            
            String isAjax = arg0.getParameter("ajax");
            String customerId = arg0.getParameter("customerId");
            
            if (isAjax != null)
                  return new ModelAndView("welcomePage", "welcomeMessage", customerId + "  " + ajaxMessage);
            
}

"welcomePage" means welcome.htm.

Can you find where is wrong?
I changed the url to

url: "HelloWorldController.java"

but it still did not work.
How to write the server side

if (isAjax != null)
                  return new ModelAndView("welcomePage", "welcomeMessage", customerId + "  " + ajaxMessage);

in order to pass the ajax message back to welcome.htm?
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account