Home > Back-end >  Can'r Fetch data from database using Axios
Can'r Fetch data from database using Axios

Time:11-20

I want to fetch data from database but can't get it and when I console print it, is just empty. Is there something wrong with my code?

         function har() {
            axios.get(url2, {
              headers: {
            'X-RapidAPI-Key': 'your-rapidapi-key',
            'X-RapidAPI-Host': 'body-mass-index-bmi-calculator.p.rapidapi.com',
              },

              })
              .then(function (response) {
               console.log(response);
   
              })
             .catch(function (error) {
               console.error(error);
              });
               }

This is my my php code

                    <?php
                    include 'db.php';



               
                    $emparray = array();

                    $sql = 'SELECT * FROM schedule_list';
                    $results = mysqli_query($conn,$sql);
                    while($row = $results->fetch_assoc()){

                        $emparray[] = $row;

                    }
                    $hey = json_encode($emparray)

                    ?>

I just want to get the data from the json encode to javascript without using react or vue, just plain vanilla javascript

CodePudding user response:

You aren't outputting anything in your server code. Try instead to output the results like:

<?php
    include 'db.php';

    $emparray = array();
    $sql = 'SELECT * FROM schedule_list';
    $results = mysqli_query($conn,$sql);
    while($row = $results->fetch_assoc()){
        $emparray[] = $row;
    }
    echo json_encode($emparray);
?>

CodePudding user response:

I think you need to actually return your data from the server. In your php code you are setting a variable called $hey to $hey = json_encode($emparray) The first thing I see is you are missing the semicolon after the variable assignment. but you also aren't returning that data. maybe try something like return json_encode($emparray);

  • Related