Angular 基于响应体改变有效载荷
我在Angular 7中与HttpClient有一个工作HTTP POST请求,如下所示,它返回用户配置文件的详细信息:Angular 基于响应体改变有效载荷,angular,rxjs,observable,Angular,Rxjs,Observable,我在Angular 7中与HttpClient有一个工作HTTP POST请求,如下所示,它返回用户配置文件的详细信息: const request { firstName: this.firstName, lastName: this.lastName, city: "Dallas" } this.http.post("URL Path", request).subscribe(response => console.log(response); 我的问题是,是否可以根据
const request {
firstName: this.firstName,
lastName: this.lastName,
city: "Dallas"
}
this.http.post("URL Path", request).subscribe(response => console.log(response);
我的问题是,是否可以根据响应体更改有效负载的值?例如,如果“城市”字段返回为空,则根据以下内容更改其值:
this.http.post("URL Path", request).subscribe(response => {
if (response.toString().includes("Null"){
request.city = "Detroit"
//Resubmit POST request
}
你可以试一试
//当iif条件为true时,将发出另一个请求
this.http.post('URL路径',request).pipe(
映射(response=>response.toString()),
mergeMap(response=>iif(()=>response.includes('null'),
this.http.post('URL路径',{request.firstName,request.lastName,'Detroit'}),
(回应)
))
);
你可以给iif()
一次机会
//当iif条件为true时,将发出另一个请求
this.http.post('URL路径',request).pipe(
映射(response=>response.toString()),
mergeMap(response=>iif(()=>response.includes('null'),
this.http.post('URL路径',{request.firstName,request.lastName,'Detroit'}),
(回应)
))
);
您可以在此用例中使用开关映射
操作符
const request = {
firstName: this.firstName,
lastName: this.lastName,
city: 'Dallas'
};
this.http.post(url, request).pipe(
switchMap(response => {
return response.toString().includes('null')
? this.http.post(url, {...request, city: 'Detroit'})
: of(response);
})
).subscribe(console.log);
您可以在此用例中使用
switchMap
操作符
const request = {
firstName: this.firstName,
lastName: this.lastName,
city: 'Dallas'
};
this.http.post(url, request).pipe(
switchMap(response => {
return response.toString().includes('null')
? this.http.post(url, {...request, city: 'Detroit'})
: of(response);
})
).subscribe(console.log);
您的代码是否有一部分不起作用?为什么要重新提交?它正在工作,但我希望能够根据响应正文中的空值更改有效负载中的值。是否有部分代码不工作?为什么要重新提交?它正在工作,但我希望能够根据响应正文中的空值更改有效负载中的值。谢谢,我会看一看!谢谢,我来看看!