Post JSON from angular 2 to php

后端 未结 3 1049
难免孤独
难免孤独 2020-12-29 15:06

I try to post data from angular 2 to php:

let headers = new Headers();
headers.append(\'Content-Type\', \'application/json\');
var order = {\'order\': this.o         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 15:32

    I do not know if it's bad practice but it seems right to me, although it bothers me a little

    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    
    const obj = { nome: 'gabriel', age: 20 };
    const body = 'data=' + JSON.stringify(obj);
    
    this.http.post('/test.php', body, { headers })
      .subscribe(res => console.log(res.json()), res => console.error(res))
    

    And in php

    $post = json_decode($_POST['data']);
    

提交回复
热议问题