首页 > 解决方案 > $_POST 中没有任何内容(Http 请求/Angular/PHP)

问题描述

我遇到了一个简单的问题,但我在网上没有找到任何答案。所以,这是我的代码

const URL = 'http://(...)/scripts/Fiabilisation_Unique.php';
const httpOptions = {
headers: new HttpHeaders({
   'Content-Type: application/json' 
   'reportProgress': 'true'
})
(...)
onClickAdresse(f : NgForm){
var adresse : Adresse= (f.value);
console.log(adresse); //{adresse : "just a french adresse"}
this.http.post(URL, adresse, httpOptions).subscribe(
  (event) => {
    this.temp=(event); // handle event here
    },
  () => {
  console.log("Observable done ! ");
    }
  );
}

但是在我的文件“Fiabilisation_Unique.php”中,当我尝试一个简单的:print_r($_POST);它打印我:Array( )

我很确定我的问题并不复杂,但我只是不知道问题出在哪里。我也知道答案不应该那么远,但是......

先感谢您 :)

标签: angulartypescript

解决方案


HttpOptions的不正确。错误的引用和reportProgress 放错了位置:

const httpOptions = {
  headers: new HttpHeaders({
   'Content-Type': 'application/json'
  },
  reportProgress: true
});

但是如果你启用reportProgress,订阅将收到来自HttpEventType.Sentto的多个事件HttpEventType.Done。但我想这就是你想要的:)


推荐阅读