Angular 发送角度POST请求后清空$\u POST

Angular 发送角度POST请求后清空$\u POST,angular,post,http-post,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.ht

我正在发送邮寄请求

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时,它总是空的

您需要使用
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)
在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)

你能记录参数吗?是的,在params中,我有Lead对象Lead{name:“ff”,phone:“ff”,text:“dd”}尝试将
内容类型
更改为json,如下所示:
'Content-Type':'application/json'
$数据在这种情况下为空,那么它就不是
x-www-form-urlencoded
API所期望的