gpt4 book ai didi

javascript - 没有连接服务时显示错误消息

转载 作者:行者123 更新时间:2023-12-03 02:47:03 25 4
gpt4 key购买 nike

我正在一个 Angular 项目中工作,我正在从服务中获取数据。如果服务未启动或无法访问它,我想显示错误消息。

服务是:

import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';


@Injectable()
export class TkgmService {

ws = 'http://10.30.1.242/MebProxy/Controllers/Service.asmx/';
il;
ilce;
mahalle;
ada;
parsel;
geomAttr;

constructor(private http: Http) {
}

getIlTum() {
if (this.il) {
return;
}
const url = this.ws + 'GetirIlTum?';

this.http.get(url).subscribe(
res => {
const parseString = require('xml2js').parseString;
parseString(this.cleanResponse(res.text()), (err, result) => {
this.il = result.ArrayOfIL.Il;
this.il.sort((a, b) => {
a = a.Ad;
b = b.Ad;
return a < b ? -1 : a > b ? 1 : 0;
});
});
}
);
}

最佳答案

您可以使用错误回调来对源可观察到的错误使用react。像这样:

import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';


@Injectable()
export class TkgmService {

ws = 'http://10.30.1.242/MebProxy/Controllers/Service.asmx/';
il;
ilce;
mahalle;
ada;
parsel;
geomAttr;

constructor(private http: Http) {
}

getIlTum() {
if (this.il) {
return;
}
const url = this.ws + 'GetirIlTum?';

this.http.get(url).subscribe(
res => {
const parseString = require('xml2js').parseString;
parseString(this.cleanResponse(res.text()), (err, result) => {
this.il = result.ArrayOfIL.Il;
this.il.sort((a, b) => {
a = a.Ad;
b = b.Ad;
return a < b ? -1 : a > b ? 1 : 0;
});
});
},
error => {
// show message or something
}
);
}

希望这有帮助。

关于javascript - 没有连接服务时显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com