Angular HttpClient“解析过程中的Http失败”
发布时间:2020-12-17 07:59:11 所属栏目:安全 来源:网络整理
导读:我尝试将Angular 4的POST请求发送到我的Laravel后端. 我的LoginService有这个方法: login(email: string,password: string) { return this.http.post(`http://10.0.1.19/login`,{email,password})} 我在LoginComponent上订阅了这个方法: .subscribe( (resp
我尝试将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'}) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |