2017-09-25 74 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' })