我正在研究小型Web应用程序,我使用angular 7和asp.net核心web api作为后端.我正在尝试使用angular发出http post请求.我的服务返回字符串(令牌),当我收到它时,我想在警告框中显示它.
我已经测试了我的服务,Postman一切都按预期工作.
从浏览器我的请求成功映射到控制器的操作方法.我在这个方法体中设置了断点,它成功返回令牌没有任何问题.
但httpclient返回错误:
[object Object]
在浏览器控制台中我看到:
POST https://localhost:44385/api/auth net::ERR_INVALID_HTTP_RESPONSE
我有两个方法(for POST和for GET)在服务类中注入组件.它们看起来像这样:
logIn(username: string, password: string) {
const encodedCredentials = btoa(username + ':' + password);
const httpOptions = {
headers: new HttpHeaders({
"Authorization": "Basic " + encodedCredentials,
"Access-Control-Allow-Origin":"*"
}),
responseType: 'text' as 'text'
};
this.http.post(this.urlBase + 'auth', null , httpOptions)
.subscribe(result => {
alert(result);
}, error => {
alert(error); // [object Object]
});
}
get(){
return this.http.get(this.urlBase + 'auth', {responseType: 'text'});
} …Run Code Online (Sandbox Code Playgroud)