闲柚
2021-10-15 18:45
采纳率: 100%
浏览 108
已结题

vue ui+SpringBoot,axios发post请求到后端时没反应

因为没用过vue,所以跟着B站视频做了一个登录页,连接自己的后端,测试接收登录页的数据。其中遇到了一个问题:用axios传参,get请求传参后台可以受到,但是post请求传form表单的数据却不行。也试过跟着别人的贴子导入使用qs也不行,有人可以指点一下我怎么解决吗?真的非常感谢

运行结果:
界面

img

get请求

img

post请求,后台收不到值,有一段警告

img

img


WARN 12488 --- [nio-8081-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.example.testvue.controller.LoginCotroller.login(java.lang.String,java.lang.String)]

请求信息:

img

img

img

前端代码:

<template>
  <div class="loginpage">
    <h3>系统登录</h3>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm"  label-width="50px" class="demo-ruleForm">
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      }
    };
  },

  created() {
    this.$axios.get('http://localhost:8081/test?name=%27123%27&age=16').then(
      res=>{
        console.log(res)
      }
    )
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.
            post('http://localhost:8081/login', this.ruleForm)
            .then(
              res=>{
                console.log(res)
            })
          alert(this.ruleForm.password);
          alert(this.ruleForm.username);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
  .loginpage{
    width: 280px;
    background-clip: padding-box;
    border-radius: 15px;
    padding: 15px 35px;
    background: #fff;
    box-shadow: 0 0 10px;
    border: 1px solid #eaeaea;
    margin: 150px auto;

  }
</style>

main.js的配置

import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'


Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.prototype.$axios = axios;

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

路由

import Vue from 'vue'
import Router from 'vue-router'
import Login from "../views/Login";

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login
    }
  ]
})

后端代码:

package com.example.testvue.controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(allowCredentials="true")
@RestController
public class LoginCotroller {

    @PostMapping("/login")
    public String login(@RequestBody String username,
                      @RequestBody String password){
        //测试接收前端axios  post请求传来的数据
        System.out.println("post请求:"+username);
        System.out.println("post请求:"+password);
        return null;
    }

    @GetMapping ("/test")
    public String test(@RequestParam String name,
                       @Validated @RequestParam String age){
        //测试接收前端axios  get请求传来的数据
        System.out.println("get请求:"+name);
        System.out.println("get请求:"+age);
        return null;
    }
}


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • been_ss 2021-10-15 19:20
    最佳回答

    哈喽,@RequestBody用Bean类接收,先建一个User类,以User去接收,获取时用User.属性获取,代码大概如下:

    @Data
    @EqualsAndHashCode(callSuper = false)
    public class User implements Serializable {
    
        private String username;
    
        private String password;
    
    }
    
    @RestController
    public class LoginCotroller {
        @PostMapping("/login")
        public String login(@RequestBody User user){
            //测试接收前端axios  post请求传来的数据
            System.out.println("post请求:"+user.username);
            System.out.println("post请求:"+user.password);
            return null;
        }
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题