javascript - php post key是json字符串
问题描述
我正在使用发送请求
function loadData(interfaceName) {
axios({
method: 'POST',
url: '<?= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ?>',
data: {
interface: interfaceName,
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(function (response) {
document.getElementById('statistics').innerHTML = JSON.stringify(response.data);
});
}
并使用 php 接收
require_once __DIR__ . '/../vendor/autoload.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
header('Content-Type: application/json');
dd($_POST);
print_r($_POST);
exit;
但是 php 会返回这个:
我该如何解决?
解决方案
当我像这样使用 axios 时,问题就解决了:
const params = new URLSearchParams();
params.append('interface', interfaceName);
axios({
method: 'POST',
url: '<?= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ?>',
data: params,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
})
推荐阅读
- python - 为什么我不能修补 multiprocessing.Process?
- c++ - C++ & Lua 在运行时注册自定义 lua 库
- simulation - Anylogic - 如何在模拟中测量在制品库存 (WIP)
- shopify - 下载 Shopify 本地开发的主题
- amazon-web-services - 如何避免 S3 存储桶策略冲突
- javascript - 如何使用(React Native)每秒更改背景颜色
- python - Python 3.3+,标准 venv 虚拟环境包有什么原因或问题吗?
- arrays - 在C中初始化后如何将列表分配给数组
- reactjs - 在 react 和 typescript 中解构 useState 中的状态
- angular - 如何在 Angular 中显示来自 JSON 对象的图像