You need to enable JavaScript to run this app. axios GET请求如何传递参数?Vue.js搭配PHP后端时出现404问题求解
优惠活动
大模型
产品
解决方案
定价
更多

axios GET请求如何传递参数?Vue.js搭配PHP后端时出现404问题求解

问题原因与解决方案

你遇到的404不是接口地址不存在,是PHP代码中SQL执行失败后主动设置的响应状态码:你的代码里if (!$result) { http_response_code(404); die(mysqli_error($con)); },只要SQL执行报错就会返回404状态。

核心错误点

  • SQL语法错误:你传入的rayon参数是字符串类型的值IT,但拼接SQL时没有给值加单引号,最终执行的SQL为select * from horaire where rayon=IT,数据库会把IT识别为字段名而非字符串值,导致SQL执行失败。
  • 参数读取不严谨:直接使用$rayon = $_GET['rayon'],如果请求没有携带rayon参数,PHP会抛出Notice级错误,也可能影响后续逻辑执行。
  • 存在SQL注入风险:直接拼接用户传入的参数到SQL语句中,存在严重的安全漏洞。
  • JSON输出逻辑冗余:手动拼接JSON数组的格式容易出错,直接读取所有查询结果后用json_encode输出更稳妥。

修正代码

PHP部分修改

$con = mysqli_connect($host, $user, $password, $dbname);

$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'] ?? '', '/'));

if (!$con) {
    // 数据库连接错误返回500状态码更合理
    http_response_code(500);
    die("Connection failed: " . mysqli_connect_error());
}

$sql = '';
switch ($method) {
    case 'GET':
        // 先判断参数是否存在,同时对参数做转义避免SQL注入
        $rayon = isset($_GET['rayon']) ? mysqli_real_escape_string($con, $_GET['rayon']) : '';
        // 字符串类型的字段值要加单引号
        $sql = "select * from horaire" . ($rayon ? " where rayon='$rayon'" : '');
        break;
    case 'POST':
        // POST参数也建议做转义或者使用预处理语句
        $numero = mysqli_real_escape_string($con, $_POST["numero"] ?? '');
        $date = mysqli_real_escape_string($con, $_POST["date"] ?? '');
        $type = mysqli_real_escape_string($con, $_POST["type"] ?? '');
        $rayon = mysqli_real_escape_string($con, $_POST["rayon"] ?? '');
        $modifNumero = mysqli_real_escape_string($con, $_POST["modifNumero"] ?? '');
        $modifDate = mysqli_real_escape_string($con, $_POST["modifDate"] ?? '');

        $sql = "insert into horaire (numero, date, type, rayon, modifNumero, modifDate) values ('$numero', '$date', '$type', '$rayon', '$modifNumero', '$modifDate')";
        break;
}

// run SQL statement
$result = mysqli_query($con, $sql);

// die if SQL statement failed
if (!$result) {
    // SQL错误属于服务端错误,返回500状态码更合理,方便前端区分是真的接口不存在还是业务逻辑错误
    http_response_code(500);
    die(mysqli_error($con));
}

if ($method == 'GET') {
    // 直接读取所有结果转JSON输出,不需要手动拼接
    $data = [];
    while ($row = mysqli_fetch_object($result)) {
        $data[] = $row;
    }
    echo json_encode($data);
} elseif ($method == 'POST') {
    echo json_encode($result);
} else {
    echo mysqli_affected_rows($con);
}

可选优化建议

建议后续使用mysqli预处理语句替代直接拼接SQL,可以彻底避免SQL注入问题,示例逻辑如下:

// GET查询预处理示例
if ($rayon) {
    $stmt = mysqli_prepare($con, "select * from horaire where rayon=?");
    mysqli_stmt_bind_param($stmt, "s", $rayon);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
} else {
    $result = mysqli_query($con, "select * from horaire");
}

你的axios代码写法没有问题,不需要修改。

内容的提问来源于stack exchange,提问作者Nicolas Henrotay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 17:45:01