我有一个简单的代码,我的PHP将查找名称为“ prod_name和prod_desc”的任何项目并打印它们。这是代码:
echo ($_POST['prod_name']);
echo ($_POST['prod_desc']);
exit;
makeRequest() {
let data = {
'prod_name': 'Celphone',
'prod_desc': 'A Big Ass Celphone'
}
let httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
responseType: 'text' as 'json'
}
this.http.post('http://localhost:8080/vysor_vendas_online/rest/post.php', data, httpOptions)
.pipe(
retry(3),
tap(console.log),
catchError(this.errorHandl)
)
.subscribe(
response => console.log(response),
error => console.error(error)
)
}
但是响应始终是:
<br />
<b>Notice</b>: Undefined index: prod_name in <b>C:\Fitgroup\ws_fg\www\vysor_vendas_online\rest\post.php</b> on line <b>3</b><br />
<br />
<b>Notice</b>: Undefined index: prod_desc in <b>C:\Fitgroup\ws_fg\www\vysor_vendas_online\rest\post.php</b> on line <b>4</b><br />
但是在Postman应用中,请求进行得很好。看:Postman image
怎么了?
您的Angular代码正在使用以下HTTP标头:
'Content-Type' : 'application/json'
向后端服务器描述您的有效负载类型,当邮递员使用完全不同的有效负载类型(表单数据)时,更改PHP代码以接受原始JSON,或使用此npm包将游览对象转换为表单数据,package