springboot security 登录接口需要将axios的post请求参数改为params

ppylox
·8/31/2021·0 阅读
AI 摘要模型根据正文自动生成,仅供参考

Axios中post请求可用params或data传参:params拼接到URL,data放入请求体,直接使用post方法默认传data。接入Spring Boot Security的formLogin登录接口时,需将Axios的post请求参数改为params方式传递,才能被后端正确接收。

正常情况下,Axios中post请求传参的params与data的两种形式


params是添加到url的请求字符串中的,用于get请求,。
data是添加到请求体(body)中的, 用于post请求。


其实,post请求也可以使用params方式传值 , 但是get请求没有data方式。


本文就来介绍一下post的两种传值方式


Axios 中posts的params与data的两种传参


第一种:data方式

this.$axios({

   url:\n'/api/user/login' ,

   method:\n'post',

   headers:\n{

      'Content-Type': 'application/json'

   },

   data:{

      username:\nthis.user,

      pwd:\nthis.pwd

   }

}).then((res) => {

  console.log(res)

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

})

 

第二种:params方式

this.$axios({

   url:\n'/api/user/login' ,

   method:\n'post',

   headers:\n{

      'Content-Type': 'application/json'

   },

   params:{

      username:\nthis.user,

      pwd:\nthis.pwd

   }

}).then((res) => {

  console.log(res)

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

})


注:直接使用post方法,默认传递的参数为 data 的方式
代码如下:

this.$axios.post('/api/user/login',{username:\nthis.user, pwd: this.pwd }),

 {

   headers:\n{

     'Content-Type': 'application/json'

   }

 }).then((res)\n=> {

\n\n\n\n\n\n\n\n\n\n\n\n

 console.log(res)

}


用post请求的时候可以同时用data跟params,当然跟后端接受有关系,总的来说,就是params就是参数拼接的方式,data就是在body里面的方式。

post请求使用params的场景可能比较少,大部分情况还是data方式。

比如接入springboot security权限,formlogin的登陆接口就是需要通过params方式获取参数。


相关文章

评论 (0)

暂无评论,来写第一条吧