【问题标题】:Token authentication with C# Web Api and axios使用 C# Web Api 和 axios 进行令牌认证
【发布时间】:2018-12-10 15:42:04
【问题描述】:

我正在实现登录。我可以在 Postman 中向端点令牌发送 post 请求,但不能在 axios 中。

axios函数:

axios({
    method: 'post',
    url: 'http://localhost:20449/token',
    headers: {
      'Content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      'grant_type': 'password',
       'username': user.username,
       'password': user.password
    }
}).then(resp => {
   console.log(resp)
   commit(AUTH_SUCCESS, resp)
   dispatch(USER_REQUEST)
   resolve(resp)
})

我得到了错误

“unsupported_grant_type”

【问题讨论】:

    标签: asp.net-web-api axios restful-authentication http-token-authentication


    【解决方案1】:

    我找到了解决方案。当data是对象时,axios默认使用application/json。即使在标头中添加 application/x-www-form-urlencoded 后它也不起作用。所以我下载了包 qs(npm install qs --save)。我导入包并使用下面的 axios 命令:

    var qs = require('qs');
    axios.post('/foo', qs.stringify({ 'bar': 123 });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      相关资源
      最近更新 更多