Angular HttpClient“解析过程中的Http失败”

前端之家收集整理的这篇文章主要介绍了Angular HttpClient“解析过程中的Http失败”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试将Angular 4的POST请求发送到我的Laravel后端.

我的LoginService有这个方法

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);

我的chrome dev工具说我的请求是200个状态代码的成功.但我的Angular代码触发了错误块并给出了我的这条消息:

“解析http://10.0.1.19/api/login期间的Http失败”

如果我从我的后端返回一个空数组,它可以工作……所以Angular试图解析我对json的响应?我怎么能这个呢?

您可以使用responseType指定要返回的数据不是JSON.有关更多信息,请参见 docs.

在您的示例中,您应该能够使用:

return this.http.post('http://10.0.1.19/login',password},{responseType: 'text'})
原文链接:https://www.f2er.com/angularjs/143756.html

猜你在找的Angularjs相关文章