Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php API从angular6中的httpClient响应中检索参数值?_Php_Json_Angular_Angular Httpclient - Fatal编程技术网

使用php API从angular6中的httpClient响应中检索参数值?

使用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

我有一个angular中的方法,它将值发布到php api,当http post成功时,我会得到一个json响应,但当我尝试访问res.status或json对象中的任何参数时,我得到的属性“status”在类型“object”上不存在。如何获取响应对象中参数的值

这是我的天使课

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)