繁体   English   中英

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

[英]Empty $_POST after sending angular post request

我正在以角度发送帖子请求

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM