401响应网络请求后的页面刷新

时间:2018-01-25 13:56:17

标签: javascript angular rxjs

我在自定义服务中有一些代码返回以下内容:

public myMethod(
    server: string,
    id: string): Observable<any> {
        const url: string = ...;

        const headers = new Headers();
        headers.append('Authorization', 'Token=' + this._token);
        const options = new RequestOptions({headers});

        const body = {
            server,
            id
        };

        return this.httpClient.post(url, body)
            .map(res => res.json())
            .catch((error: any) => Observable.throw(error.json()));
    }

当我订阅此调用时,我按以下方式处理错误:

this.myService.myMethod(server, id)
    .subscribe(data => {
        this.x = 'hello';
        this.y = 'world';
        this.z = '!';

        data.propertyOne = this.myForm.get('someProperty').value;
        data.propertyTwo = this.myForm.get('someOtherProperty').value;

        this.myForm.patchValue({
            key: data;
        });

        this.myEventEmitter.emit(someValue);
        this.myOtherEventEmitter.emit(someOtherValue);
    }, (err) => {
        if (err.Code === 401) {
            event.preventDefault(); // is this needed?
            // some more stuff here
        }
    });

最终会发生什么(这就是我尝试使用event.preventDefault())的原因,而我不确定这是否是因为401,页面是否刷新而不是处理错误优雅并继续执行程序。

这是一个常见问题吗?关于如何绕过它的任何想法?

由于

0 个答案:

没有答案