首页 > 解决方案 > 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 会返回这个:

输出

我该如何解决?

标签: javascriptphpaxios

解决方案


当我像这样使用 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',
    }
})

推荐阅读