【问题标题】:How To Send Request Authorization Content-type application/x-www-form-urlencoded如何发送请求授权 Content-type application/x-www-form-urlencoded
【发布时间】:2019-02-13 09:38:30
【问题描述】:

大家好。我想请求获得令牌。但它不起作用

我的代码 Angular 7

const url = "http://localhost:50729/token", body = 'username=admin&password=admin&grant_type=password';
const requestHeader = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
this.http.post(url, body, { headers: requestHeader }).toPromise().then(res => {
  //result
  console.log(res.json());
}).catch(x => alert("Username Or PassWord Incorrect!"));

哪里错了?如何让它工作!谢谢大家!

【问题讨论】:

    标签: angular authorization httprequest token


    【解决方案1】:

    第一点是,如果您使用来自 '@angular/common/http' 的 HttpClient,则不必执行 res.json(),因为它不再需要(我猜是 angular v4.3 起)

    第二点是您可能正在尝试发送FormData。试试:

    const body = new FormData();
    body.append('username', 'admin')
    body.append('password', 'admin')
    body.append('grant_type', 'password')
    

    第三点是尝试将Content-Type 设置为undefined 以让HttpClient 根据正文参数自动设置标题。

    应该是这样的。

    【讨论】:

      猜你喜欢
      • 2019-06-14
      • 1970-01-01
      • 2019-02-04
      • 2013-11-10
      • 2016-02-18
      • 2019-10-26
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多