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中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
