简体   繁体   中英

How to call spring controller method through ajax

Here is my Controller method:

@RequestMapping( value={"/home"},params="userAction=loadHomePage",method=Request.POST)
public String viewPage(@ModelAttribute("dataForm") formData, Model model,HttpServletRequest req)throws Exception{
...
}

How to pass the params value through ajax ?

Something like this.. Let me know if further guidance is needed..

var request= new XMLHttpRequest();
        request.onreadystatechange= function(){
             if (request.readyState == 4 && request.status == 200) {

             }
        };
        request.open("POST", "/home", true);
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send("userAction=loadHomePage");
    };

To set the model object:

    var formData= new FormData();
    formData.append('username' , 'payal');
    formData.append('id' , 123456);
    request.send(formData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM