vue請求傳數據到后台處理數據的方法


vue中使用axios來與后台建立連接

  • 在axios使用post請求中Content-Type: application/json是默認的,對於這個Content-Type,在后台中,我們需要用@RequestBody來處理數據,具體如下

前端傳遞數據

axios.post("http://localhost:8080/Login", {username : this.sizeForm.username}).then(res=>{
  console.log(res)
})

后端接收數據
實體類

package com.google.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class user {
    String name;
    String password;
}

Controller


@CrossOrigin(origins = "http://localhost:8081",maxAge = 3600)  //解決跨域問題
@Controller
public class SecurityController {

@RequestMapping("/Login")
    @ResponseBody
    public HashMap<String, Object> Login( @RequestBody user user, Model model) {
        String username = user.getName();
        String password = user.getPassword();
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("username",username);
        userMap.put("password",password);
        return userMap;
    }
}

注意這里有跨域問題
我們需要在該類上添加注釋@CrossOrigin


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM