【发布时间】:2018-12-21 04:51:34
【问题描述】:
我正在尝试在 Angular 中发出 http post 请求,但不知道为什么它不起作用。
getTripEstimate(event){
let authUsername:string = '*****';
let authPassword:string = '*****';
let headers = new Headers();
let params = {
"grant_type": "*****",
"scope": "*****",
"username": "*****",
"password": "*****"
};
let body = JSON.stringify(params)
console.log("body: "+body)
this.http.post("http://localhost:8000/auth/oauth/token",{
headers: headers,
body: body
}).toPromise().then(res => console.log(res.json)).catch(res => console.log("Error"))
}
我已经注释掉了密码和用户名,但是我收到了 401 错误,并且不知道为什么我使用的凭据是正确的,因为它们是通过邮递员工作的。也许我发出的 POST 请求不正确?
【问题讨论】:
-
您可以尝试使用 Postman 来查看是否能够在 Angular 之外创建有效的 http post 请求。然后尝试在 Angular 中复制它,看看请求是否符合 Chrome 网络开发工具。
-
我确实这样做了。并尝试在 Angular 中进行复制,但我想我没有从上面粘贴的代码中正确地做到这一点? ^
-
应该是
new HttpHeaders() -
我添加了 import { HttpHeaders } from '@angular/common/http';并从 new Headers() 更改为 new HttpHeaders() 但它仍然不起作用
-
构造函数是
httpClient.post(url, body, options),选项是你的标题
标签: angular http http-post http-get