php - How can I transfer parameters with axios and get method?
Get the solution ↓↓↓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 !
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.