Fetch request with params(使用参数获取请求)
问题描述
我将数据提取到服务器,我想在我发送的控制台参数中查看.但是在 PHP 端 $_POST 值是空的,我总是收到空数组.(WEB -> PHP -> WEB)我做错了什么?
I fetch data to server, and I want to see in the console params, which i send. But on PHP side $_POST value is empty and I always recieve empty array.(WEB -> PHP -> WEB) What I do wrong ?
JS代码:
function json(response){
return response.json()
}
let data = {obj : 'value'};
fetch('http://localhost/Fetch_mysql_angular/requests.php', {
method: 'post',
headers: {
'Accept': 'application/json',
"Content-type": "application/json"
},
body: JSON.stringify(data)
})
.then(json)
.then(function (data) {
console.log(data);
})
.catch(function (error) {
console.log('Request failed', error);
});
PHP 代码:
<?php
echo json_encode($_POST);
?>
控制台
[]
感谢您花时间在我的帖子上!
Thanks for spending time on my post!
推荐答案
当您使用 JSON.stringify(data) 时,您的数据将被编码并以 json 格式而不是表单数据发送.所以 php 不会填充 $_POST 变量.
When you using JSON.stringify(data) your data will be encoded and sent in json format and not form-data. So php will not fill $_POST variable.
你仍然可以通过阅读php://input获取POST json数据
You can still get POST json data by reading php://input
$data = json_decode( file_get_contents('php://input') ) ;
var_dump( $data );
这篇关于使用参数获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用参数获取请求


- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01