ssm framework front-end interface ajax no return problem

  Problem: The blogger encountered an embarrassing problem when writing code. The ajax code that runs well on others will never receive the return value on his own project.

  

$.ajax({
            url:'loginAdd',
            type:'post',
            data:login,
            success:function(){
                alert(111);
            }
        });

 

controller layer

    @RequestMapping(value="loginAdd",method=RequestMethod.POST)
    @ResponseBody//Code inserted later
    public String add(Login login,Model m) {
        //loginService.save(login);
        return "redirect:/index.jsp";
    }

 

  After some hard work, I found that ajax viewed the network response through the post request and found that the code returned, but it returned a view. So by this time, the blogger is even more embarrassed, so after some research:

  It is found that the return value of the method in the controller layer is returned as a view, not as a response message.

So the blogger added

  @ResponseBody

1、

  The function of the @responseBody annotation is to convert the object returned by the controller method into the specified format through an appropriate converter, and then write it to the body area of ​​the response object, which is usually used to return JSON data or XML

  Data, it should be noted that after using this annotation, it will not go to the processor, but directly write the data to the input stream. Its effect is equivalent to outputting the data in the specified format through the response object.

2、  

  @RequestMapping("/login")
  @ResponseBody
  public User login(User user){
    return user;
  }
  User field: userName pwd
  Then the data received in the foreground is: '{"userName":"xxx","pwd": "xxx"}'

  is equivalent to the following code:
  @RequestMapping("/login")
  public void login(User user, HttpServletResponse response){
    response.getWriter.write(JSONObject.fromObject(user).toString());
  }

 Reprinted to: https://www.cnblogs.com/qiankun-site/p/5774325.html

and finally came to a conclusion: adding @ResponseBody before the method of the controller layer in the ajax request, the method canThe
parameters as the response message, not the view interface

1、

  The function of the @responseBody annotation is to convert the object returned by the controller method into the specified format through an appropriate converter, and then write it to the body area of ​​the response object, which is usually used to return JSON data or XML

  Data, it should be noted that after using this annotation, it will not go to the processor, but directly write the data to the input stream. Its effect is equivalent to outputting the data in the specified format through the response object.

2、  

  @RequestMapping("/login")
  @ResponseBody
  public User login(User user){
    return user;
  }
  User field: userName pwd
  Then the data received in the foreground is: '{"userName":"xxx","pwd": "xxx"}'

  is equivalent to the following code:
  @RequestMapping("/login")
  public void login(User user, HttpServletResponse response){
    response.getWriter.write(JSONObject.fromObject(user).toString());
  }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889018&siteId=291194637