大佬教程收集整理的这篇文章主要介绍了rest – Angular 2如何从后端读取自定义错误消息,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试了以下代码,没有任何帮助:
诺言:
this._http.post('/login',{email: 'email@example.com',password: '123'}) .toPromise() .then((resp) => console.log(resp),(error) => console.log(error));
观察到:
this._http.post('/login',password: '123'}) .subscribe(response =>console.log(responsE),(error) => console.log(error));
从后端我发送响应作为文本,对于OK或Unauthorized,对于OK我发回String token == UUID.randomUUID().toString();,对于错误我发回消息如String error =“无效的凭据“;问题是console.log工作并打印文本以便成功(在这种情况下是令牌),但是如果出现错误,它只是打印:响应状态:200表示URL:null.
如果我将代码更改为JSON.Stringify(错误),我得到这样的东西:
{"_body":{},"status":401,"ok":false,"statusText":"Unauthorized","headers":{"null":["http/1.1 401 Unauthorized"],"Access-Control-Allow-Headers":["Origin"," X-requested-With"," Content-Type"," Accept"," Referer"," User-Agent"],"Access-Control-Allow-Met hods":["POST"," GET"," PUT"," deletE"," OPTIONS"],"Access-Control-Allow-Origin":["*"],"Allow":["*"],"Content-Length":["36"],"Content-Type":["text/plain; charset=utf-8"],"Date":["Tue"," 23 Aug 2016 14:53:25 GMT"]},"type":2,"url":null}
正如你所看到的,甚至在Object内部都没有提到错误测试!!
{ "message": "invalid email or password" }
我可以在_body中得到结果,我只能这样读:console.log(error._body.messagE)!但我觉得这样做有些不对劲,我不想在这种情况下作为json回应.
对于angularjs(角度1),它只是打印响应,一切都很酷,而在角度2,这是一个真正的问题.
问题是什么,以及如何在没有任何重构后端的情况下解决这个问题?
编辑:
我正在使用Angular 2.0.0-rc.4和http:`“@ angular / http”:“2.0.0-rc.4”
以上是大佬教程为你收集整理的rest – Angular 2如何从后端读取自定义错误消息全部内容,希望文章能够帮你解决rest – Angular 2如何从后端读取自定义错误消息所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。