简体   繁体   English

如何将Ajax传递给Spring Controller

[英]How to pass ajax into a spring controller

Am getting values in Ajax page 我正在Ajax页面中获取值

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 但是Controller页面获取空值... Ajax数据值未传递给控制器

@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. 您应该在控制器函数的参数中使用@RequestBody批注。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestBody.html 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 如果storeName只是一个字符串,则可以使用@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 在Ajax通话中,您可以像

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

and remove below property in Ajax call 并在Ajax调用中删除以下属性

data : JSON.stringify(data), 

Your Ajax will look like below: 您的Ajax如下所示:

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 "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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