php - Angular 7 - http 帖子 - 标题内没有发送 PAYLOAD
问题描述
我刚刚创建了一个 Angular 7 项目
我尝试发送带有一些数据的帖子,标题部分没有设置任何内容
所以在服务器端我只调用了 php 脚本,$_POST 数组中没有任何内容
此代码在 angular 5 中工作正常,我应该在 chrome 中看到标题日志中的数据
createPostOptions() {
let headers = new Headers({
'Content-Type': 'application/json',
});
let options = new RequestOptions({ headers: headers, withCredentials: true });
return options;
}
getParts(): Observable<any>
{
return this.http.post('http://localhost/site/data.php',{command:'getParts'}, this.createPostOptions())
.pipe(map((response: Response) => {
return this.processData(response,this.router);
}));
}
php代码:
function cors()
{
header("HTTP/1.1 " . "200" . " " . "OK");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header('Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, X-Requested-With, X-API-KEY, X-Auth-Token, X-Requested-With, Authorization, Content-Range, Content-Disposition, Origin, Access-Control-Request-Method');
header('Access-Control-Max-Age: 86400');
header('Access-Control-Allow-Origin: '."http://localhost");
header('Access-Control-Allow-Credentials: true');
}
//-----------------------------------
if($_SERVER['REQUEST_METHOD']=="OPTIONS")
{
cors();
}
else
{
...
}
解决方案
由于您在正文中发送 json,但不是作为 post 参数,因此您需要类似
$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;
推荐阅读
- python - Plotly:格式化 y 轴刻度和文本标签
- go - Go中的array和array[:]有区别吗?
- python - 用一组字符替换字符串中的“#”,给出所有可能的字符串组合
- kubernetes - 为什么 kubernetes 使用 bost Rest API 和 gRPC?
- javascript - AlpineJS:在里面使用哪个 x-on 指令
- javascript - 结帐后如何将摩洛哥国家代码添加到wp woocomerce中的电话号码?
- r - 有效过滤掉所有列连续为零的位置
- python - 无法显示 pyspark 数据帧,即使它很小('.show()' 问题)
- reactjs - DeepL 客户端站点使用情况
- azure - 用于将数据库从一个订阅复制到另一个订阅的 Azure 角色