axios发送表单数据到后台

axios默认发送post请求时候参数是json形式传递到后台,后台(SpringMvc)需要通过添加注解@RequestBody映射到对象的方式来接收,后台开发人员认为我就2个参数,懒得再封装个对象了,让前端同事直接通过表单的形式传递。于是就有了如下解决办法:

const form = new FormData();
form.append('username', this.username);
form.append('password', this.password);
form.append('rememberMe', this.rememberMe+'');
const data = await instance.post('/auth/authorize', new URLSearchParams(form));
axios
  .post(process.env.BASE_API_LOGIN + '/auth/authorize', new URLSearchParams(form))
  .then(function (response) {
    console.log(response.data);
  });

猜你喜欢

转载自blog.csdn.net/catcher92/article/details/86530802