如何在Angular 2中处理200以外的http状态代码

编程入门 行业动态 更新时间:2024-10-07 16:19:39
本文介绍了如何在Angular 2中处理200以外的http状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

现在我执行http请求的方式(从此答案中借用)是:

Right now the way I do http requests (borrowed from this answer) is this:

POST(url, data) { var headers = new Headers(), authtoken = localStorage.getItem('authtoken'); headers.append("Content-Type", 'application/json'); if (authtoken) { headers.append("Authorization", 'Token ' + authtoken) } headers.append("Accept", 'application/json'); var requestoptions = new RequestOptions({ method: RequestMethod.Post, url: this.apiURL + url, headers: headers, body: JSON.stringify(data) }) return this.http.request(new Request(requestoptions)) .map((res: Response) => { if (res) { return { status: res.status, json: res.json() } } }); }

这正常工作,除了以下事实之外:如果返回的状态码不是200,则angular2将会失败.例如,如果用户要发布内容,并且服务器返回400,则angular 2将抛出异常:/p>

This works fine, except for the fact that angular2 will fail if the status code returned is anything else than 200. For example, if a user wants to post something and the server returns 400, angular 2 will throw the exception:

未捕获的异常:[对象对象]

uncaught exception: [object Object]

如何避免这种情况?我想在我的应用中处理这些状态代码,以增强用户体验(显示错误等)

How can I avoid this? I'd like to handle these status codes in my app in order to enhance user experience (show errors, etc)

推荐答案

是的,您可以使用catch操作符进行处理,并根据需要显示警报,但首先必须以这种方式导入Rxjs

Yes you can handle with the catch operator like this and show alert as you want but firstly you have to import Rxjs for the same like this way

import {Observable} from 'rxjs/Rx'; return this.http.request(new Request(this.requestoptions)) .map((res: Response) => { if (res) { if (res.status === 201) { return [{ status: res.status, json: res }] } else if (res.status === 200) { return [{ status: res.status, json: res }] } } }).catch((error: any) => { if (error.status === 500) { return Observable.throw(new Error(error.status)); } else if (error.status === 400) { return Observable.throw(new Error(error.status)); } else if (error.status === 409) { return Observable.throw(new Error(error.status)); } else if (error.status === 406) { return Observable.throw(new Error(error.status)); } }); }

还可以处理.map函数时由catch块引发的错误(带有err块),

also you can handel error (with err block) that is throw by catch block while .map function,

像这样-

... .subscribe(res=>{....} err => {//handel here});

更新

根据任何状态的需要而无需检查特定状态,您可以尝试以下操作:-

Update

as required for any status without checking particluar one you can try this: -

return this.http.request(new Request(this.requestoptions)) .map((res: Response) => { if (res) { if (res.status === 201) { return [{ status: res.status, json: res }] } else if (res.status === 200) { return [{ status: res.status, json: res }] } } }).catch((error: any) => { if (error.status < 400 || error.status ===500) { return Observable.throw(new Error(error.status)); } }) .subscribe(res => {...}, err => {console.log(err)} );

更多推荐

如何在Angular 2中处理200以外的http状态代码

本文发布于:2023-10-10 18:24:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   代码   如何在   Angular   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!