如何从 Angular 中的 http post 请求获取原始 JSON 输出?
How do I get the raw JSON output from an http post request in Angular?
在下面,我调用后端 api 来验证用户登录和 returns 错误或简单 JSON 输出中的成功
export class AuthService {
constructor(private http: HttpClient) { }
getUserDetails(email, password): Observable<any> {
// post to API server - return user info if valid
return this.http.post('http://localhost/api/myapp/task_login.php', {
email,
password
})
.pipe(
map((response:Response)=>response.json())
);
}
}
我在网上遵循了大量不同的指南,最终得到了这个,试图让它简单地用 php 文件响应的任何内容进行回复。我只想要原始输出,这样我就可以对其采取适当的行动。我从这样的组件文件中调用它:
var response:any = this.Auth.getUserDetails(email, password);
console.log(response);
if (response.success)
$location.path('/home');
当我查看控制台时,我得到某种 Observable 对象,它似乎实际上并不包含我需要的数据(我能找到,那是一个深层对象)。无论请求回复什么,我如何正确 return ?
你的方法 getUserDetails
returns 一个 Observable。您需要订阅 Observable 才能获取响应对象。
this.Auth.getUserDetails(email, password)
.subscribe(res => {
// do something with your object
console.log(res);
}
);
例如。
您可以在 angular 文档中阅读有关 Observable 的信息:https://angular.io/guide/observables or on the Library (RxJS) homepage: https://rxjs-dev.firebaseapp.com/guide/observable
编辑
此外,您的服务中不需要管道:
getUserDetails(email, password): Observable<any> {
return this.http.post('http://localhost/api/myapp/task_login.php',
{
email,
password
});
}
我认为代码应该修改。
export class AuthService {
constructor(private http: HttpClient) { }
getUserDetails(email, password): Observable<any> {
// post to API server - return user info if valid
return this.http.post('http://localhost/api/myapp/task_login.php', {
'email':email,
'password':password
})
.pipe(
map((response:Response)=>response.json())
);
}
此外,您可以使用 "Subscribe" 进行回复。
this.Auth.getUserDetails(email, password).subscribe((data)=>{
console.log(data);
}, (err)=>console.log(err));
在下面,我调用后端 api 来验证用户登录和 returns 错误或简单 JSON 输出中的成功
export class AuthService {
constructor(private http: HttpClient) { }
getUserDetails(email, password): Observable<any> {
// post to API server - return user info if valid
return this.http.post('http://localhost/api/myapp/task_login.php', {
email,
password
})
.pipe(
map((response:Response)=>response.json())
);
}
}
我在网上遵循了大量不同的指南,最终得到了这个,试图让它简单地用 php 文件响应的任何内容进行回复。我只想要原始输出,这样我就可以对其采取适当的行动。我从这样的组件文件中调用它:
var response:any = this.Auth.getUserDetails(email, password);
console.log(response);
if (response.success)
$location.path('/home');
当我查看控制台时,我得到某种 Observable 对象,它似乎实际上并不包含我需要的数据(我能找到,那是一个深层对象)。无论请求回复什么,我如何正确 return ?
你的方法 getUserDetails
returns 一个 Observable。您需要订阅 Observable 才能获取响应对象。
this.Auth.getUserDetails(email, password)
.subscribe(res => {
// do something with your object
console.log(res);
}
);
例如。
您可以在 angular 文档中阅读有关 Observable 的信息:https://angular.io/guide/observables or on the Library (RxJS) homepage: https://rxjs-dev.firebaseapp.com/guide/observable
编辑 此外,您的服务中不需要管道:
getUserDetails(email, password): Observable<any> {
return this.http.post('http://localhost/api/myapp/task_login.php',
{
email,
password
});
}
我认为代码应该修改。
export class AuthService {
constructor(private http: HttpClient) { }
getUserDetails(email, password): Observable<any> {
// post to API server - return user info if valid
return this.http.post('http://localhost/api/myapp/task_login.php', {
'email':email,
'password':password
})
.pipe(
map((response:Response)=>response.json())
);
}
此外,您可以使用 "Subscribe" 进行回复。
this.Auth.getUserDetails(email, password).subscribe((data)=>{
console.log(data);
}, (err)=>console.log(err));