rro @net :: ERR_INVALID_HTTP_RESPONSE após a solicitação após o Angular 7
Estou trabalhando em um aplicativo Web pequeno e usando a API da Web angular 7 e asp.net core como back-end. Estou tentando fazer uma solicitação de postagem http com angular. Meu serviço retorna string (token) e, quando recebê-lo, quero mostrá-lo na caixa de alert
Testei meu serviço comPostman
e tudo funciona como esperad
No navegador, minha solicitação é mapeada com sucesso para o método de ação do controlador. Eu defini o ponto de interrupção neste corpo do método e ele retorna com êxito o token sem nenhum problem
Mas o httpclient retorna erro:
[object Object]
E no console do navegador, vejo o seguinte:
POST https://localhost:44385/api/auth net::ERR_INVALID_HTTP_RESPONSE
Tenho dois métodos (paraPOST
e paraGET)
em uma classe de serviço injetada no componente. Eles se parecem com estes:
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'});
}
O que pode ser problema?
Atualizar
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
error: ProgressEvent
bubbles: false
cancelBubble: false
cancelable: false
composed: false
currentTarget: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
defaultPrevented: false
eventPhase: 0
isTrusted: true
lengthComputable: false
loaded: 0
path: []
returnValue: true
srcElement: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
target: XMLHttpRequest {__zone_symbol__xhrSync: false, __zone_symbol__xhrURL: "https://localhost:44385/api/auth", __zone_symbol__loadfalse: null, __zone_symbol__errorfalse: null, __zone_symbol__xhrListener: ƒ, …}
timeStamp: 80234.59999999614
total: 0
type: "error"
__proto__: ProgressEvent
headers: HttpHeaders
headers: Map(0) {}
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure response for (unknown url): 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: null
__proto__: HttpResponseBase
Update 2:
Pedido com Carteiro:
Update 3: tentativa de obter o token várias vezes mostrou que, às vezes, a pós-solicitação é bem-sucedida e às vezes não .