小编pub*_*lic的帖子

使用Angular 7发布请求后,net :: ERR_INVALID_HTTP_RESPONSE错误

我正在研究小型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)

asp.net-core-webapi angular angular7

13
推荐指数
1
解决办法
5470
查看次数

标签 统计

angular ×1

angular7 ×1

asp.net-core-webapi ×1