我尝试从 Angular 4 向我的 Laravel 后端发送一个 POST 请求。
我的 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 开发工具显示我的请求成功,状态码为 200。但是我的 Angular 代码触发了 error
块并给了我这个消息:
解析 http://10.0.1.19/api/login 时的 Http 失败
如果我从后端返回一个空数组,它可以工作……所以Angular试图将我的响应解析为JSON?我怎样才能禁用它?
原文由 nutzt 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用
responseType
指定要返回的数据 不是 JSON。在您的示例中,您可以使用
responseType
字符串值text
:responseType
的完整选项列表是:json
(默认)text
arraybuffer
blob
有关更多信息,请参阅 文档。