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 7.4 在写入变量中的 Twig 问题 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php中的PDF导出 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01