Angular 8 Http POST,内容类型为 application/x-www-form-urlencoded

2 http-post angular8 x-www-form-urlencoded

问这个问题似乎有点奇怪,但我无法弄清楚。

我已经浏览过 stackoverflow 上的链接和谷歌,事实上尝试了很多东西,但我无法得到解决方案。下面是一些值得一提的

Angular 6 HTTP 客户端发布 - 错误请求

如何强制 Angular2 使用 x-www-form-urlencoded 进行 POST

问题 - api 的请求选项始终为 400(错误请求)

见解 -

下面是在另一个项目上运行的代码

  const headers = new HttpHeaders({
    'Content-Type': 'application/x-www-form-urlencoded'
  });
  const body = new HttpParams()
    .set('grant_type', 'password')
    .set('username', stateData.username)
    .set('password', stateData.password)
    .set('client_id', 'XXXXX-5B3C-4A7D-WEW-23BWWWF9B7872'); //Letters are dummy here for security reasons

//URL preceded by https
return this.http.post('abc.com/authtoken', body , {headers: headers }).pipe(
  map(res => {
    return res;
  })
 );
Run Code Online (Sandbox Code Playgroud)

现在,当我在另一个项目中尝试相同的代码时,它不会传递 OPTIONS 请求,抛出 400(错误请求)。

我们可以假设其余的事情都是为了使这项工作顺利进行。问题主要出在代码片段上。

最令人惊奇的是,当我在 url 中添加一个尾部斜杠(如 - authtoken/ )时,它似乎通过了 OPTIONS 200OK,但在原始 POST 请求中失败为 404,这是可以理解的,因为 authtoken/ 不存在。

怎样才能通过呢?我有点困惑不知道要做什么。

如前所述,我已经尝试了很多方法来使其发挥作用。请建议

我们正在使用 Angular 8.2.13

谢谢

小智 6

   const headers = new HttpHeaders().set(
     'Content-Type',
    'application/x-www-form-urlencoded;'
   );

   body = 'grant_type=' + password + '&username=' + setData.username + '&password=' + 
   setData.password + '&Client_Id=' + 'XXXXX-5B3C-4A7D-WEW-23BWWWF9B7872';

   return this.http.post('abc.com/authtoken', body , {headers: headers })
Run Code Online (Sandbox Code Playgroud)