使用php API从angular6中的httpClient响应中检索参数值?
我有一个angular中的方法,它将值发布到php api,当http post成功时,我会得到一个json响应,但当我尝试访问res.status或json对象中的任何参数时,我得到的属性“status”在类型“object”上不存在。如何获取响应对象中参数的值 这是我的天使课使用php API从angular6中的httpClient响应中检索参数值?,php,json,angular,angular-httpclient,Php,Json,Angular,Angular Httpclient,我有一个angular中的方法,它将值发布到php api,当http post成功时,我会得到一个json响应,但当我尝试访问res.status或json对象中的任何参数时,我得到的属性“status”在类型“object”上不存在。如何获取响应对象中参数的值 这是我的天使课 export class QuizComponent implements OnInit { constructor(private http: HttpClient) { } myData = { param
export class QuizComponent implements OnInit {
constructor(private http: HttpClient) { }
myData = { param1: 'This is param 1', param2: 'this is param 2' }
sendmydata(){
const req = this.http.post('http://myhost.com/phpapi/api.php',this.myData)
.subscribe(
res => {
console.log(res);
// how can I access res.status here?
res.status;//this line says Property 'status' does not exist on type 'Object'
},
err => {
console.log("Error occured");
}
);
}
这是我的PHP:
(我知道一些事先准备好的声明,只是在这里保持简单):
假设为响应定义了接口:
interface Response {
status: string;
}
将类型信息添加到您的post
呼叫:
this.http.post<Response>('http://myhost.com/phpapi/api.php',this.myData)
this.http.post('http://myhost.com/phpapi/api.php,此为.myData)
或任何类型(如果没有可用的类型定义)
this.http.post<any>('http://myhost.com/phpapi/api.php',this.myData)
this.http.post('http://myhost.com/phpapi/api.php,此为.myData)
this.http.post<any>('http://myhost.com/phpapi/api.php',this.myData)