axios 从Vue3发布请求到php不起作用

yzuktlbb  于 2023-01-17  发布在  iOS
关注(0)|答案(1)|浏览(129)

我有一个问题与axios发送请求在vue到php.当我使用post,并试图发送数据到后端,没有数据发送.我该怎么办?这段代码到底是错误的?这是我的代码:

axios
  .post('http://localhost:8888/vueBackend/connection.php',{
     titleName:'Default'
  })
  .then(response => console.log(response))
  .catch(err=>console.log(err.message))

这是回应:

这是我的后端代码:

$server = "localhost";
$dbname = "Vue";
$username = "root";
$password = "";
$tblGroup = "Titles";
$dsn = "mysql:host=$server;dbname=$dbname;charset=utf8";
$id = '';
$option = [
    PDO::ATTR_PERSISTENT => TRUE,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$method = $_Server['REQUEST_METHOD'];
try{
    $connection = new PDO($dsn, $username, $password, $option);
    if($connection){
        try{
            $stmt = $connection->prepare("INSERT $tblGroup SET titleName=?");
            $stmt->bindValue(1,$_POST['titleName']);
            $stmt->execute();
            echo "Successful";
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        
    }
}catch(PDOException $exception){
    echo"Error Connect To Database : [ " . $exception->getMessage() . " ] ";

}

我希望将titleName发送到后端,php获取titleName值并将其添加到数据库中。

w41d8nur

w41d8nur1#

你可以在POST变量之前尝试一下

$_POST = json_decode(file_get_contents("php://input"),true);

相关问题