2017-09-25 134 views
8

我嘗試從Angular 4發送POST請求到我的Laravel後端。Angular HttpClient「解析期間的Http失敗」

我的login服務具有此方法:

login(email: string, password: string) { 
    return this.http.post(`http://10.0.1.19/login`, {email, password}) 
} 

我認購我LoginComponent這種方法:

.subscribe(
     (response: any) => { 
      console.log(response) 
      location.reload() 
     }, 
     (error: any) => { 
      console.log(error) 
     } 
) 

這是我Laravel後端方法:

... 

if($this->auth->attempt(['email' => $email, 'password' => $password], true)) { 
    return response('Success', 200); 
} 

return response('Unauthorized', 401); 

我的鉻開發工具說,我的請求是一個成功的200狀態代碼。但我的角碼觸發error塊,給我這個消息:

"Http failure during parsing for http://10.0.1.19/api/login"

如果我從我的後端,它的工作原理返回一個空數組...所以角度試圖分析我的反應到JSON?我如何disble這個?

回答

20

您可以使用responseType指定要返回的數據不是JSON。有關更多信息,請參閱docs

在你的榜樣,你應該能夠使用:

return this.http.post(`http://10.0.1.19/login`, {email, password}, {responseType: 'text'}) 
0

,如果你有選擇

return this.http.post(`${this.endpoint}/account/login`,payload, { ...options, responseType: 'text' })