我正在用角度发送发帖请求
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时,它总是空的
答案 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)