[英]angular2 http.post to a turned off server => return error on Android BUT NOT ON IOS
I am using Angular 2, Ionic 2. 我正在使用Angular 2,Ionic 2。
I shut down the server my app sends its request to, in order to check how the offline mode is managed. 我关闭了我的应用发送请求的服务器,以便检查离线模式的管理方式。
I have made a Custom HTTP Service for my project based on import { Http, Headers, Response, RequestOptions, RequestMethod, Request } from '@angular/http'
. 我已根据import { Http, Headers, Response, RequestOptions, RequestMethod, Request } from '@angular/http'
为我的项目制作了自定义HTTP服务。 It includes a 'function sendRequest': 它包括'function sendRequest':
public sendRequest = (data):Observable<Response> => {
// console.log("CustomHttpService->sendRequest() starts");
let headersToUse = new Headers();
headersToUse.append("Content-type",'application/x-www-form-urlencoded');
data = this.jsonToURLEncoded(data);
let options = { headers:headersToUse };
return this.http.post(this.url, data, options).map(
(res:Response)=>{
return res.json();
}
).catch(
this.handleErrorObservable
);
}
And the 'function handleErrorObservable': 'function handleErrorObservable':
private handleErrorObsevable (error:Response|any){
console.log("handleError error.message " + error.message);
console.log("handleError error " + error);
return Observable.throw(error.message || error);
}
In Android it works, after a certain time I get in the log: 在Android中它可以工作,在一段时间后我进入日志:
handleError error Response with status 0 for URL: null handleError错误URL状态为0的响应:null
But on IOS the error never arrives, like if there was no time out set. 但是在IOS上,错误永远不会到来,就像没有时间设置一样。 Any tips? 有小费吗?
I personally prefer to use this fomart of @angular/http to handle errors and it works throughout. 我个人更喜欢使用@ angular / http的这个fomart来处理错误,它始终有效。
this.http.post(url, data)
.map(res => res.json())
.subscribe((data) => {
// Use returned data
},
(err) => {
// Handle the error here
});
The (err)
part helps handle all network related errors even when server is offline. 即使服务器处于脱机状态, (err)
部分也可以帮助处理所有与网络相关的错误。 Hope this helps. 希望这可以帮助。
I solved it by adding a custom timeout setting on the observable like in that SO question , in my function. 我在我的函数中通过在observable中添加自定义超时设置来解决它,就像在SO问题中一样。 It looks like that now: 现在看起来像这样:
public sendRequest = (data):Observable<Response> | Observable<any> => {
// console.log("CustomHttpService->sendRequest() starts");
let headersToUse = new Headers();
headersToUse.append("Content-type",'application/x-www-form-urlencoded');
data = this.jsonToURLEncoded(data);
let options = { headers:headersToUse };
return this.http.post(this.url, data, options).timeoutWith(CustomHttpService.TIMEOUT_DELAY,Observable.throw(new Error(CustomHttpService.ERROR_REQ_HTTP))).map(
(res:Response)=>{
return res.json();
}
)
.catch(this.handleErrorObservable)
}
private handleErrorObservable (error:Response|any){
console.error(CustomHttpService.CLASS_TAG + " handlerErrorObservable: error.message " + error.message);
return Observable.throw(error.message);
}
Also it requires to add on the top of the class some specific import to use the functions timeoutWith
and Observable.throw
: import 'rxjs/add/operator/timeoutWith
and import 'rxjs/add/observable/throw
like it is explained in the official Angular2 doc . 此外,它需要在类的顶部添加一些特定的导入来使用函数timeoutWith
和Observable.throw
: import 'rxjs/add/operator/timeoutWith
并import 'rxjs/add/observable/throw
就像官方解释的那样Angular2 doc 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.