简体   繁体   中英

How to pass ajax into a spring controller

Am getting values in Ajax page

function GetInfoDivision()
{
     var data = $("#storeName").val();
    $.ajax({
        type : "POST",
        contentType : "application/json",   
        url : "hello",
        data : JSON.stringify(data),    
        dataType : 'json',              
        //timeout : 100000, 
        success : function(map) {
            console.log("SUCCESS: ", data);
            display(data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });

But Controller page getting null value ...Ajax data value not passed to the controller

@RequestMapping(value="/hello", method = RequestMethod.POST)
        public String employeeLogin(ModelMap model, HttpServletRequest request) {
         String sname = request.getParameter("storeName");   
         System.out.println("s="+sname);    
            shopModel s = new shopModel();
              s.setStoreName(sname);

            //boolean result = employeeService.employeeLogin(employee);
              boolean result =false;
             if(result == true){

                    model.addAttribute("message", "Successfully logged in.");
             }
             else
             {
                model.addAttribute("message", "Username or password is wrong.");
            }
            return "redirect:index.jsp";
        }

看到

You should use the @RequestBody annotation in the parameter of the controller function.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestBody.html

@Autowired
private HttpServletRequest request;

@RequestMapping(value="/hello", method = RequestMethod.POST)
public String employeeLogin(@RequestBody ModelMap model) {      

If storeName is just a string then you can use @RequestParam

@RequestMapping(value="/hello", method = RequestMethod.POST)
        public String employeeLogin(ModelMap model, HttpServletRequest request,
@RequestParam(value = "storeName", required = false) String storeName) {

String sname = storeName;
}

and in Ajax call you can have call like

url : "hello" + "?storeName=" + data

and remove below property in Ajax call

data : JSON.stringify(data), 

Your Ajax will look like below:

function GetInfoDivision()
{
     var data = $("#storeName").val();
    $.ajax({
        type : "POST",
        contentType : "application/json",   
        url : "hello" + "?storeName=" + data,
        dataType : 'json',              
        //timeout : 100000, 
        success : function(map) {
            console.log("SUCCESS: ", data);
            display(data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });
@RequestMapping(value = "hello", method = RequestMethod.POST)
@ResponseBody
public String methodname(@RequestParam("data") String data) {
    ...
    return "";
}

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