[英]Empty $_POST after sending angular post request
I am sending post request in angular 我正在以角度发送帖子请求
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});
}
but when I am checking $_POST on php side it is always empty 但是当我在php端检查$ _POST时,它总是空的
You need to use the HttpParams
to set your values 您需要使用HttpParams
设置值
const body = new HttpParams()
.set('name', 'foo')
.set('phone', 'bar')
.set('text', 'baz')
and that you send as body, HttpClient
will take care of the headers 并以正文形式发送时, HttpClient
将处理标头
return this.http.post('http://127.0.0.1/saveLead', body)
in your ts file: 在您的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);
}
in your php: 在您的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.