亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Angular http 攔截器

發布時間:2020-07-26 17:30:04 來源:網絡 閱讀:178 作者:csboyty 欄目:web開發

Angular http的攔截器一般用來處理每個http都需要添加的參數或者是統一處理錯誤信息

Angular1.x的http攔截器處理:

 $httpProvider.interceptors.push(function ($q) {
                return {
                    request: function (config) {

                        var url = config.url;

                        //這個token表示是在登錄狀態, 不要用在header中,options無法設置header
                        if(TASApp["x-auth-token"]){
                            if(url.indexOf("?") == -1){
                                url+="?x-auth-token="+TASApp["x-auth-token"];
                            }else{
                                url+="&x-auth-token="+TASApp["x-auth-token"];
                            }
                        }

                        config.url = url;

                        return config || $q.reject(config);
                    },

                    response: function (res) {
                        //統一處理返回信息,如果是錯誤則在這里統一處理,注意如果這樣處理錯誤(reject),那么所有的錯誤信息會進入http的error回調,在success里默認就是成功,都可以不判斷data.success
                        if (res.data.success == false) {
                            TASApp.ajaxReturnErrorHandler(res.data["info"]); //TASApp是一個constant對象
                            return $q.reject(res.data); //will go to error callback
                        } else if (res.data.success == "relogin") {
                            TASApp.relogin();
                            return $q.reject(res.data); //will go to error callback
                        } else {
                            return res; //will go to success callback
                        }
                    },

                    responseError: function (res) {
                                                //統一處理請求沒發成功的錯誤
                        TASApp.ajaxErrorHandler();
                        return $q.reject(res);
                    }
                };
            });

Angular2.x的http攔截器處理:


export class AddHttpHeaderInterceptor implements HttpInterceptor {

    constructor(private formService: FormService, private formHelper: FormHelper, private message: NzMessageService, private lang: Lang) {
    }

    intercept(req: HttpRequest<any>, next: HttpHandler) {

        // set  X-Requested-With that serve need to for ajax
        let clonedReq = req.clone({headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')});
        if (this.formService.currentUser) {
            //options http can not add x-auth-token, use param
            //clonedReq = req.clone({headers: req.headers.set('x-auth-token', this.formService.currentUser['x-auth-token'])});

            //global add param x-auth-token and
            clonedReq = req.clone({params: req.params.set('x-auth-token', this.formService.currentUser['x-auth-token']),
                headers: req.headers.set('X-Requested-With', 'XMLHttpRequest')});
        }

        // ===========================================================
        // global handle error
        // ===========================================================
        return next.handle(clonedReq).pipe(
            catchError(this.formService.handleError),
            //handle success false
            filter(res => {
            if(res['statusText'] && res['statusText'] === 'OK'){
                if(res['body'] && (res['body']['success'] == false || res['body']['success']=='relogin')){
                    if(res['body']['success'] == 'relogin'){
                        //handle relogin here, can add some message
                        (<any>window).location.href = this.formHelper.getBaseUrl()+'login';
                        return false;
                    } else {
                        //if no info will have a code
                        this.message.error(res['body']['info'] || this.lang.lang["errorCode"][res['body']['code']]);
                        console.log(res);
                        //if return false will not trigger subscribe function, if you need trigger return true
                        return true;

                        /*
                        this will fire subscribe error handle, that means if backend error will go to subscribe->error,
                        subscribe->next is only handle backend success, bug if services use like MyShares/getFormInfo that will have problem,
                        need add error handle for every http request and run handler function
                        this.http.get(url).subscribe(obj=>{handle when backend success},error=>{handle when backend error}
                        */
                        //throw new Error("error");
                    }
                }
            }
            return true;
        })
        );
    }
}

@NgModule({
    declarations: [

    ],
    imports: [

    ],
    providers: [
        {provide: HTTP_INTERCEPTORS, useClass: AddHttpHeaderInterceptor, deps: [FormService, FormHelper, NzMessageService, Lang], multi: true},
        {provide: APP_INITIALIZER, useFactory: loginAndInitForm, deps: [FormService], multi: true},
        {provide: NZ_I18N, useValue: zh_CN},
        {provide: NZ_MODAL_CONFIG, useValue: {autoBodyPadding: true}},
        {provide: NZ_MESSAGE_CONFIG, useValue: {nzDuration: 3000}}
    ],
    bootstrap: [AppComponent]
})
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武宁县| 鱼台县| 建德市| 枣庄市| 贡觉县| 朝阳县| 恩施市| 惠州市| 平塘县| 开江县| 南郑县| 平远县| 永济市| 肃南| 措美县| 哈巴河县| 定日县| 怀远县| 涞源县| 峨山| 房产| 青神县| 罗甸县| 盱眙县| 大田县| 宁南县| 利津县| 潢川县| 哈尔滨市| 德格县| 晋州市| 仁布县| 张家口市| 东明县| 嵊州市| 淮阳县| 汉源县| 连平县| 房产| 佳木斯市| 金坛市|