0

I'm new to Vue.js and I'm trying to use it as front end while using only php to request my database.

I'm trying to use axios like this to request data from another php page :

getHoraires: function() {
                axios.get('api/horaires.php')
                    .then(function(response) {
                        console.log(response.data);
                        app.horaires = response.data;

                    })
                    .catch(function(error) {
                        console.log(error);
                    });
            },

I'd like to add one parameter in my request but I'm having an error 404 doing so :

getHoraires: function() {
                axios.get('api/horaires.php', {
                    params: {
                        rayon: 'IT'
                    }
                })
                    .then(function(response) {
                        console.log(response.data);
                        app.horaires = response.data;

                    })
                    .catch(function(error) {
                        console.log(error);
                    });
            },

Here's the code i'm using in my php page :

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

$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'], '/'));
//$input = json_decode(file_get_contents('php://input'),true);


if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}


switch ($method) {
    case 'GET':
        $rayon = $_GET['rayon'];
        $sql = "select * from horaire" . ($rayon ? " where rayon=$rayon" : '');
        break;
    case 'POST':
        $numero = $_POST["numero"];
        $date = $_POST["date"];
        $type = $_POST["type"];
        $rayon = $_POST["rayon"];
        $modifNumero = $_POST["modifNumero"];
        $modifDate = $_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) {
    http_response_code(404);
    die(mysqli_error($con));
}

if ($method == 'GET') {
    if (!$rayon) echo '[';
    for ($i = 0; $i < mysqli_num_rows($result); $i++) {
        echo ($i > 0 ? ',' : '') . json_encode(mysqli_fetch_object($result));
    }
    if (!$rayon) echo ']';
} elseif ($method == 'POST') {
    echo json_encode($result);
} else {
    echo mysqli_affected_rows($con);
}

Does anybody have a clue ? Tanks a lot !

  • what's the error that you are getting? – Amaarockz Aug 12 '21 at 15:42
  • check this => https://stackoverflow.com/questions/52561124/body-data-not-sent-in-axios-request – Amaarockz Aug 12 '21 at 15:43
  • Try to isolate the problem first and use browser devtools for debugging, currently only you can do that. 404 means... 404. Not found. If you do requests to wrong address, that's the problem. If you do them to seemingly correct address, then there's a mistake in server code, which isn't shown. `axios.get('api/horaires.php')` - this is relative url. – Estus Flask Aug 12 '21 at 15:46

0 Answers0