发送角度发布请求后,清空$ _POST

时间:2018-08-18 23:10:37

标签: angular post http-post

我正在用角度发送发帖请求

public saveLead(name: string, phone: string, text: string){
        const params: Lead = new Lead(name,phone,text);

        const headers = {
            'Content-Type':'application/x-www-form-urlencoded'
        };

        return this.http.post('http://127.0.0.1/saveLead', params, {headers});
    }

但是当我在php端检查$ _POST时,它总是空的

2 个答案:

答案 0 :(得分:0)

您需要使用HttpParams来设置值

const body = new HttpParams()
  .set('name', 'foo')
  .set('phone', 'bar')
  .set('text', 'baz')

并且您以正文形式发送,HttpClient将负责标题

return this.http.post('http://127.0.0.1/saveLead', body)

答案 1 :(得分:0)

在您的ts文件中:

public saveLead(name: string, phone: string, text: string){
    const params: Lead = new Lead(name,phone,text);
    return this.http.post('http://127.0.0.1/saveLead', params);
}

在您的php中:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$data = json_decode(file_get_contents("php://input"));
var_dump($data)