【发布时间】:2017-08-28 07:32:44
【问题描述】:
我正在研究 Angular2。我有一个登录 url,我必须使用它进行发布操作。在 Postman 中,我从 url 获得响应。但是在我尝试的代码中,我得到了 401 状态 代码,错误是 授权标头中的用户凭据无效。 但我在标题中提供了正确的用户凭据。
public getData(data: string) : Promise<any> {
if(typeof(this.login_data) === "undefined") {
let headers = new Headers();
headers.append("Authorization", "Basic " + btoa(this.username + ":" + this.password));
headers.append("Content-Type", "application/json");
headers.append("Accept", "application/json");
headers.append("id", this.custid);
let rOptions = new RequestOptions({
headers: headers,
body : data
});
return this.http.post(this.loginUrl, rOptions)
.toPromise()
.then(res => {
this.login_data = res.json().response;
return this.login_data;
})
.catch(this.handleError);
} else {
return Promise.resolve(this.login_data);
}
}
getData 方法在我的服务类中。使用下面的代码调用该方法。
this.service.getData(JSON.stringify(requestBody))
.then((response: Response) => {
if (response.status === 200){
//if success do next
}
}).catch( error => {
this.error_message = error.error;
console.log("error in component and error is: " + JSON.stringify(error));
} );
在请求正文中我正在传递这个。
let requestBody = {};
let request = {};
request['username'] = this.user.username;
request['password'] = this.user.password;
request['id'] = this.custid;
requestBody['request'] = request;
谁能帮我解决这个问题。这里有什么问题?
【问题讨论】:
标签: angular post authorization