$ _POST中没有任何内容(Http请求/ Angular / PHP)

时间:2018-06-29 08:58:15

标签: angular typescript

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

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( )

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

预先感谢您:)

2 个答案:

答案 0 :(得分:0)

您的HttpOptions不正确。错误的报价和reportProgress放错了位置:

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

但是,如果您启用reportProgress,则订阅将从HttpEventType.SentHttpEventType.Done接收多个事件。但是我想这就是您想要的:)

答案 1 :(得分:0)

好的,大家终于找到了答案。不出所料,这真的很容易。

我只是在我的PHP代码中添加了$angularJSData = json_decode(file_get_contents("php://input"));

谢谢大家!