Answer: the response data format between Axios and spring MVC is different

CSDN Q & A 2022-01-27 01:06:55 阅读数:917

answer response data format axios

In the use of axios and springmvc When doing a small project with front and rear ends separated , encounter ,springmvc The data returned in response is json Format , But in vue When the scaffold is analyzed, it becomes string

this.$"login", this.loginForm,{ emulateJSON:true,responseType:'json'}).then((response) => { // If not converted here, it will become a string , Other methods are not known for the time being  var res=JSON.parse(; alert(typeof;// This is shown as string type  if (res.flag) { this.$message.success(" Login successful "); this.$router.push({ path: "/home" }); // Storage user object  window.sessionStorage.setItem("user",res.user) } else { this.$message.error(" Login failed "); } }); Back end java Code :```java @Autowired private UserService userService; @RequestMapping(value = "/login",produces = "application/json;charset=UTF-8") @ResponseBody public String login(@RequestBody User loginUser){ System.out.println(loginUser); User user = userService.login(loginUser); HashMap<String, Object> res = new HashMap<String, Object>(); boolean flag=false; if (user != null) { flag=true; } res.put("flag",flag); res.put("user",user); String resJson= JSON.toJSONString(res); System.out.println(resJson); return resJson; }


Refer to the answer 1:

Go straight back to map object , All the data is encapsulated in map Inside .

public String login(@RequestBody User loginUser){ Change it to public Map login(@RequestBody User loginUser){

Refer to the answer 2:

Refer to the answer 3:

It's obvious okay , If you log in successfully, what you return to the front end is the string ? What else do you want ?json It's just a data format , He's not the type


Refer to the answer 4:
copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you.