我的代码:
在service.ts中:
postRequest(path, body, header): Observable<Object> {
var needToken = this.utilityService.needAccessToken();
if (needToken) {
var tokenRequest = {};
tokenRequest[Static.TOKEN_REQUEST_USERNAME] = localStorage.getItem("this.encrypted_emailid");
tokenRequest[Static.TOKEN_REQUEST_PASSWORD] = localStorage.getItem("this.encrypted_password");
var tokenReqs = tokenRequest
var tokenHeaderJSON = {};
tokenHeaderJSON[Static.HEADER_CONTENT_TYPE] = Static.HEADER_APPLICATION_JSON;
var header: any = new Headers(tokenHeaderJSON);
this.tokenRequest(Static.TOKEN_URL, body, header).subscribe(
response => {
var message = response[0].json;
var status = response[0].status;
if (status == 200) {
this.utilityService.tokenrequest(path, body, header);
}
},
error => {
var message = error[0].json;
var status = error[0].status;
}, // error
() => {
}) // complete
}
else {
var status: number;
var message: any;
var reason;
var url = this.appendURL(path);
var headString = JSON.stringify(header);
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'application/json', 'Access-Control-Allow-Methods': 'POST' });
let option = new RequestOptions({ headers: header });
var bodyString = JSON.stringify(body);
return this.http.post(url, body, option).map(
(res: Response) => {
var res_string = JSON.stringify(res);
var json_res = JSON.parse(res_string);
var fin_json_res = json_res._body;
status = res.status;
return [{ status: res.status, json: fin_json_res }];
})
.catch((err: any) => {
var err_string = JSON.stringify(err);
var json_err = JSON.parse(err_string);
var fin_json_err = json_err._body;
var jsonErr = JSON.parse(fin_json_err);
status = err.status;
message = jsonErr.message;
return Observable.throw([{ status: err.status, json: fin_json_err }]);
});
}
}
tokenRequest(path, body, header): Observable<Object> {
var status: number;
var message: any;
var reason;
var url = this.appendURL(path);
var headString = JSON.stringify(header);
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'application/json', 'Access-Control-Allow-Methods': 'POST' });
let option = new RequestOptions({ headers: header });
var bodyString = JSON.stringify(body);
return this.http.post(url, body, option).map(
(res: Response) => {
var res_string = JSON.stringify(res);
var json_res = JSON.parse(res_string);
var fin_json_res = json_res._body;
status = res.status;
return [{ status: res.status, json: fin_json_res }];
})
.catch((err: any) => {
var err_string = JSON.stringify(err);
var json_err = JSON.parse(err_string);
var fin_json_err = json_err._body;
var jsonErr = JSON.parse(fin_json_err);
status = err.status;
message = jsonErr.message;
return Observable.throw([{ status: err.status, json: fin_json_err }]);
});
}
在component.ts中:
getAllThemes() {
var tokenReqs = this.utilityService.apitimestampClub();
var tokenHeaderJSON = {};
tokenHeaderJSON[Static.HEADER_CONTENT_TYPE] = Static.HEADER_APPLICATION_JSON;
var header = new Headers(tokenHeaderJSON);
var Body = {};
Body[Static.REQUEST_APITS] = tokenReqs;
var body = JSON.stringify(Body);
this.appComponent.spinFlag = true;
this.postToServer(header, body);
}
postToServer(header, body) {
this.utilityService.postRequest(Static.THEME_URL, body, header).subscribe(
response => {
var message = response[0].json;
var status = response[0].status;
var msg = JSON.stringify(response);
var parsed_Msg = JSON.parse(message);
var arr_Parsed_Msg = parsed_Msg.Themes;
if (status == 200) {
this.utilityService.consoleFunction(message);
}
},
error => {
var message = error[0].json;
var status = error[0].status;
if (status == 440 || status == 401) {
this.router.navigate(['/home']);
this.appComponent.spinFlag = false;
}
else {
//else block
}
}, () => {
//complete
})
}
这里,在发布请求之前检查是否需要令牌请求?如果是,那么需要在this.tokenRequest(...)行中调用令牌请求发布请求,并且从响应中它需要继续我成功请求令牌请求后请求响应的正常发布请求。在我的组件中,getAllThemes()需要等到令牌请求成功,然后是正常的post请求响应,但在我的情况下,它会调用令牌请求但不等待此请求的响应,在响应之前它返回到getAllThemes( )请求,因此未定义错误。
一般来说,如何在帖子请求和主要帖子请求中订阅另一个帖子请求需要等到以角度2完成子帖子请求?
有人可以为此提供帮助吗?